我需要找到一種方法來獲取用戶t/f答案到數組中,以便可以根據密鑰對它們進行檢查。我現在正在得到一個永無止境的循環。我不知道我該如何避免這種情況。獲取2維數組的用戶字符輸入
public class TrueFalse {
public static void main(String[] args) {
DecimalFormat mine = new DecimalFormat("##0.0");
Scanner input = new Scanner(System.in);
//Creates an array to store student answers
char [][] studentAnswers = new char[8][11];
System.out.println(" please enter T/F");
for (int h = 0; h < studentAnswers[h].length; h++){
for (int k = 0; k < studentAnswers.length; k++){
studentAnswers[k][h] = input.next().charAt(0);
}
}
//Answer Key that will be compared to student answers
char [] keys = {'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'T', 'F', 'F'};
//Grading the user answers
for (int i = 0; i < studentAnswers.length; i++){
//Grade for one student
int score = 0;
for (int j = 0; j < studentAnswers[i].length; j++){
if(studentAnswers[i][j] == keys[j])
++score;
}
double average = (double)score/studentAnswers.length * 100;
System.out.println("Student number " + i + " score was: " + mine.format(average));
}
}
}
您的代碼看起來不錯迄今。你想怎麼做這個比較,你想要什麼樣的輸出? –
我需要確保我可以從用戶那裏獲得t/f輸入,並將它們與密鑰進行比較,以便我可以獲取正確答案並輸出平均值。 – CamronT
我的代碼的其餘部分在這裏:{for(int i = 0; i
CamronT