![](/img/trans.png)
[英]Having user input name then answers to a 12 multiple choice quiz, then grading it
[英]Making a quiz with Java using arrays. Need pointers with grading answers
這是為了上課。 我無法弄清楚如何最好地將用戶輸入與數組答案鍵進行比較,從而對給出的答案進行評分。 我嘗試搜索了一段時間,但找不到我需要的東西,所以任何指針都將不勝感激!
練習的提示是:
編寫一個 DMV 程序,對駕照考試的書面部分進行評分。 它應該有20個多項選擇題。 它應該要求用戶為 20 個問題中的每一個問題輸入學生的答案,這些問題應該存儲在另一個數組中。 輸入學生的答案后,程序應顯示一條消息,指示學生通過或未通過考試。(學生必須正確回答 20 個問題中的 15 個才能通過考試)。 然后它應該顯示正確回答問題的總數,以及錯誤回答問題的總數。 輸入驗證:只接受字母 A、B、C 或 D。
到目前為止我的代碼:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] answerkey = {"b","d","a","a","c","a","a","d","b","b","b","d","c","a","c","c","a","d","a","a"};
int n = 0;
int correct = 0;
int incorrect = 0;
String answer = "";
for (int i = 0; i < 20; i++){
System.out.println("Please enter your answers. Acceptable input is limited to A,B,C and D.\n");
answer = input.next();
if (answer.compareTo(answerkey[0])==0){
correct++;}
else {incorrect++;}
}
if (correct > 14){
System.out.println("You passed.");
} else {
System.out.println("You failed.");
}
System.out.println("You have " + correct + " correct answers.");
System.out.println("You have " + incorrect + " incorrect answers.");
}
使用動態索引訪問變量。 現在,您的每個答案都將與第一個答案(“b”)進行比較
這方面的一個例子是
String[] myArray = { //initialize values here
};
for (int index = 0; index <= myArray.length-1; index++){
if (answer.equals(myArray[index]){
correct++;}
else{
incorrect++;}
}
嘗試使用此代碼將用戶輸入與數組myArray
進行比較。
希望這可以幫助。
String[] myArray= { //initialize values here
};
if ((myArray[0]).equals(answer)){
correct++;
} else{
incorrect++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.