[英]Java - How to populate 2d array with nested while loops?
我需要通过仅使用嵌套的while循环使用用户输入来填充双精度数组。 这是我到目前为止的内容:
public static double[][] score() {
int col = 3;
int row = 3;
int size = 0;
Scanner in = new Scanner(System.in);
double[][] scores = new double[row][col];
System.out.println("Enter your scores: ");
while (in.hasNextDouble() && size < scores.length) {
while (size < scores[size].length) {
scores[][] = in.hasNextDouble();
size++;
}
return scores;
}
最常见的方法是通过for
循环,因为它们允许您以简洁的方式指定所需的索引计数器:
for(int i = 0; i < scores.length; i++){
for(int j = 0; j < scores[i].length; j++){
scores[i][j] = in.nextDouble();
}
}
如果您特别需要使用while
循环,则可以做几乎相同的事情,它只是分解为多行:
int i = 0;
while(i < scores.length){
int j = 0;
while(j < scores[i].length){
scores[i][j] = in.nextDouble();
j++;
}
i++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.