Пользовательский ввод java 2d ArrayJAVA

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

Сообщение Anonymous »

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

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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

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