Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据输入(for/while语句?)询问变量的#_Java_Variables_If Statement_While Loop - Fatal编程技术网

Java 根据输入(for/while语句?)询问变量的#

Java 根据输入(for/while语句?)询问变量的#,java,variables,if-statement,while-loop,Java,Variables,If Statement,While Loop,是否有一种方法可以要求用户根据先前的用户输入输入一定数量的变量。我不想为每一个可能的用户输入都做一个if语句,因为这既麻烦又乏味 我向用户询问网格大小,然后我想让他们在每个框中填入一个数字 我想我可以使用for或while语句不断向用户询问新的数字,直到达到网格的高度和宽度,但我不知道如何一次只询问一个变量,然后在下次询问用户时(下次循环时)更改变量名称 我希望它能像这样工作: for(int i=0; i<height; i++){ System.out.print("Enter

是否有一种方法可以要求用户根据先前的用户输入输入一定数量的变量。我不想为每一个可能的用户输入都做一个if语句,因为这既麻烦又乏味

我向用户询问网格大小,然后我想让他们在每个框中填入一个数字

我想我可以使用for或while语句不断向用户询问新的数字,直到达到网格的高度和宽度,但我不知道如何一次只询问一个变量,然后在下次询问用户时(下次循环时)更改变量名称

我希望它能像这样工作:

for(int i=0; i<height; i++){
    System.out.print("Enter next number");
    (variable+1)=scan.nextInt();
}
for(int i=0;i针对这种情况的两种“食谱”解决方案是:

针对这种情况的两种“食谱”解决方案是:


当您想要存储一大堆相关项目时,可以考虑使用数组(如果您知道有多少)或
列表(如果您不知道的话)。不能像Java中那样递增变量名。如果要存储多个输入,可以将每个输入添加到
列表
或数组中以供以后使用。如果要存储一整组相关项,请考虑使用数组(如果知道有多少)或
列表
(如果不知道)。不能像Java中那样递增变量名。如果要存储多个输入,可以将每个输入添加到
列表或数组中,以供以后使用。
int[] numbers = new int[height];
for(int i = 0; i < height; i++) {
    System.out.print("Enter next number: ");
    numbers[i] = scan.nextInt();
}
List<Integer> numbers = new ArrayList<>(height);
for(int i = 0; i < height; i++) {
    System.out.print("Enter next number: ");
    numbers.add(scan.nextInt());
}