Пользовательский ввод Java 2D массивJAVA

Программисты JAVA общаются здесь
Anonymous
Пользовательский ввод Java 2D массив

Сообщение Anonymous »

Мне нужно создать код, который принимает пользовательский ввод для создания размера 2D массива (строки и столбцы должны быть менее 6), а затем заполните его значениями, которые дает пользователь (между -10 и 10). Мой цикл для принятия ценностей вообще не работает, и я не уверен, почему. Вот код < /p>

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.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);
}


Подробнее здесь: https://stackoverflow.com/questions/360 ... a-2d-array

Вернуться в «JAVA»