Benutzereingabe Java 2D -ArrayJava

Java-Forum
Anonymous
 Benutzereingabe Java 2D -Array

Post by Anonymous »

Ich muss Code erstellen, mit dem Benutzereingaben die Größe des 2D -Arrays erstellt werden (Zeilen und Spalten müssen weniger als 6 sein) und dann mit Werten ausfüllen, die der Benutzer angibt (zwischen -10 und 10). Meine Schleife für die Einnahme der Werte funktioniert überhaupt nicht und ich bin mir nicht sicher, warum. Hier ist der Code < /p>

Code: Select all

    import java.util.Scanner;

public class Matrix {

public static void main(String[] args) {
// TODO Auto-generated method stub

// Implement scanner
Scanner input = new Scanner(System.in);

// create loop for accepting matrix input
// first accept row size
System.out.println("Please enter the number of rows in your matrix.      Must be 5 or less.");
int row = input.nextInt();
while (row > 5 || row < 1) {
System.out.println("Sorry. Your number is not the correct size. "
+ "Please enter the number of rows in your matrix. Must be between 5 and 1.");
row = input.nextInt();
}
// next accept column size
System.out.println("Please enter the number of columns in your matrix. Must be 5 or less.");
int column = input.nextInt();
while (column > 5 || column < 1) {
System.out.println("Sorry. Your number is not the correct size. "
+ "Please enter the number of columns in your matrix. Must be between 5 and 1.");
column = input.nextInt();
}
// declare array with row and columns the user gave
int[][] userArray = new int[row][column];

// create loop for accepting values within the matrix
// first loop for row values
for (int i = 0; i < userArray.length; i++) {
System.out.println("Please enter numbers between -10 and 10 for your matrix rows");
int rowValues = input.nextInt();
while (rowValues > 10 || column < -10) {
System.out.println(
"Sorry. Your number is not the correct size. " + "Please enter a number between 10 and -10.");
rowValues = input.nextInt();
}

// second embedded loop for column values
for (int j = 0; j < userArray[i].length; j++) {
System.out.println("Please enter numbers between -10 and 10 for your matrix columns");
int columnValues = input.nextInt();
while (columnValues > 10 || column < -10) {
System.out.println("Sorry. Your number is not the correct size. "
+ "Please enter a number between 10 and -10.");
columnValues = input.nextInt();
}
}
}

printMatrix(userArray);
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post