Results 1 to 2 of 2
- 12-08-2011, 08:13 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 1
- Rep Power
- 0
Creating 2D array from all user input
Hello,
I'm working on a project and I need to write a program that asks the user for the 2 dimensions, and then the program asks the user to enter all the elements by row and column which I've successfully done but I can't figure out how to print out the finished matrix. Here's what I have so far:
import java.util.Scanner;
public class Num_1 {
public static void main (String[] args) {
Scanner scanner = new Scanner (System.in);
int rows;
int columns;
System.out.println("Enter number of rows: ");
rows = scanner.nextInt();
System.out.println ("Now enter the number of columns: ");
columns = scanner.nextInt();
int [][] matrix = new int [rows] [columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println("Enter the number for row " + i + " and column " + j + " : ");
matrix [i][j] = scanner.nextInt();
}
}
}
}
- 12-08-2011, 08:16 PM #2
Re: Creating 2D array from all user input
You could use a nested loop with the outer loop iterating through the rows and the inner loop thru the columns.how to print out the finished matrix.
Use the print method for each element on a row and use println to go to the next row when a row is finished.
Similar Threads
-
Creating an object based on user input parameters
By monkeyhead in forum New To JavaReplies: 5Last Post: 11-14-2011, 11:59 PM -
Creating a number of objects based on user input.
By Kevinius in forum New To JavaReplies: 21Last Post: 04-03-2011, 08:53 AM -
Making a loop and creating variables based off of user input
By seanfmglobal in forum New To JavaReplies: 2Last Post: 01-13-2011, 05:43 AM -
Creating a Table with user input
By JonniBravo in forum EclipseReplies: 1Last Post: 09-08-2010, 12:50 PM -
Creating a dialog to input user/password
By prfalco in forum New To JavaReplies: 4Last Post: 02-18-2008, 07:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks