Reputation:
I need to populate a double array with user input by using nested while loops only. This is what I have so far:
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;
}
Upvotes: 0
Views: 5208
Reputation: 909
The most common way to do this is through for
loops since they allow you to specify the index counters you need in a concise way:
for(int i = 0; i < scores.length; i++){
for(int j = 0; j < scores[i].length; j++){
scores[i][j] = in.nextDouble();
}
}
If you specifically need to use while
loops you can do pretty much the same thing, its just broken up into multiple lines:
int i = 0;
while(i < scores.length){
int j = 0;
while(j < scores[i].length){
scores[i][j] = in.nextDouble();
j++;
}
i++;
}
Upvotes: 4