在方法totalCorrect()和totalIncorrect()中,我设置了一个var累加器来计算有多少个问题是对还是错。由于某些原因,它将无法正确增加。该程序说用户得到了24或40个正确和不正确的信息...为什么它不能正确递增?
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class driverExam {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Driver's Test. Input your answers for the following
10 Q's. ");
System.out.println();
String[] testAnswers = {"B","D","A","A","C","A","B","A","C","D"};
int uT = testAnswers.length;
String[] userAnswers = new String[uT];
int i =0;
while(i<uT) {
System.out.print("Question #"+(i+1)+": ");
String userInput = in.nextLine();
userInput = userInput.toUpperCase();
if (userInput.equals("A")|| userInput.equals("B")||
userInput.equals("C")
|| userInput.equals("D")) {
userAnswers[i] = userInput;
i++;
}else {
System.out.println("Enter again: ");
}
}
System.out.println("Here are the results.");
int qRight = totalCorrect(userAnswers,testAnswers);
System.out.println("Total Correct: "+qRight);
int qWrong = totalIncorrect(userAnswers, testAnswers);
System.out.println("Total Incorrect: "+qWrong);
System.out.println(passed(qRight));
}
仅在数组发现userAnswers中的String在testAnswers中相同且在后一种方法中的var“ wrong”相同时,我才尝试增加var“ same”。
/**
* @param user
* @param test
* @return
*/
public static int totalCorrect(String[] user,String[] test) {
int same=0;
for (int r = 0;r<=user.length-1;r++) {
for (int k =0;k<=test.length-1;k++) {
if(user[r].equals(test[k])) {
same++;
}
}
}
return same;
}
/**
* @param user
* @param test
* @return
*/
public static int totalIncorrect(String[] user,String[] test) {
int z=0;
int wrong = 0;
boolean isValid = true;
while (isValid && z<user.length) {
if (user[z]!=test[z]) {
isValid = false;
wrong ++;
z++;
}
}
return wrongQ;
}
/**
* @param numRight
* @return //returns whether student has passed or failed
*/
public static String passed(int numRight) {
int goldenNum = 8;
if (numRight >=goldenNum) {
return ("You passed.");
}else
return ("You have failed.");
}
/**
* @param user
* @param test
* @return
*/
public static int[] questionMissed(String[] user, String[] test) {
//return array which display which questions were missed
}
}
答案 0 :(得分:1)
您正在检查每个用户答案是否与任何测试答案匹配。您只能将其与单个答案进行比较:
public static int totalCorrect(String[] user,String[] test) {
int same=0;
for (int r = 0;r<=user.length-1;r++) {
if(user[r].equals(test[r])) {
same++;
}
}
return same;
}
总的错误答案应该只是所有答案和正确答案之间的差,因此您不需要为此使用单独的方法。
int qWrong = userAnswers.length - qRight;
答案 1 :(得分:0)
您只需要1个循环即可检查2个数组(如果它们的大小相同):
public static int totalCorrect(String[] user,String[] test) {
int qRight = 0;
for (int i = 0;i<user.length;i++) {
if(user[i].equals(test[i])) {
qRight++;
}
}
return qRight;
}
public static int totalIncorrect(String[] user,String[] test) {
int qWrong=0;
for (int i = 0;i<user.length;i++) {
if(!user[i].equals(test[i])) {
qWrong++;
}
}
return qWrong;
}
或者您可以重复使用totalCorrect
方法:
public static int totalIncorrect(String[] user,String[] test) {
int qRight=totalCorrect(user,test);
return test.length - qRight;
}