我在代码的最后阶段遇到问题,我想将输入的答案与用户存储的答案进行比较。用例是这样的。用户应输入一个带有正确答案的问题。然后,另一个用户(在这种情况下是相同的)应该能够阅读问题并提供答案。如果答案匹配,则过程完成,然后继续下一个问题。
当我比较答案时,它似乎一直工作到最后。当我调试用例时,它的读取值具有相同的值。
抱歉,很长的代码段
public static Scanner input = new Scanner(System.in);
public static ArrayList<String> questions = new ArrayList();
public static ArrayList<String> answers = new ArrayList();
public static void main(String[] args) {
start();
}
public static void start(){
System.out.println("---------------------------------------------");
System.out.println("-- !Hello and welcome to date! --");
System.out.println("-- --");
System.out.println("-- 1. Question --");
System.out.println("-- 2. Answer --");
System.out.println("-- --");
System.out.println("-- --");
System.out.println("---------------------------------------------");
int choice = input.nextInt();
switch (choice){
case 1: question();
break;
case 2: answer();
break;
}
}
public static void question(){
System.out.println("---------------------------------------------");
System.out.println("-- !Question! --");
System.out.println("-- --");
System.out.println("-- 1. Add question --");
System.out.println("-- 2. Read questions --");
System.out.println("-- 3. Amount of questions --");
System.out.println("-- --");
System.out.println("-- 5. Go back --");
System.out.println("---------------------------------------------");
int choice = input.nextInt();
input.nextLine();
switch (choice){
case 1:
System.out.println("Type your question here!");
questions.add(input.nextLine());
System.out.println("What is the answer to the question?");
answers.add(input.nextLine());
question();
break;
case 2:
System.out.println(questions);
question();
break;
case 3:
System.out.println(questions.size());
question();
break;
case 5:
start();
break;
default:
question();
break;
}
}
public static void answer(){
System.out.println("---------------------------------------------");
System.out.println("-- !Answer! --");
System.out.println("-- --");
System.out.println("-- 1. Answer --");
System.out.println("-- --");
System.out.println("-- 5. Go back --");
System.out.println("---------------------------------------------");
int choice = input.nextInt();
input.nextLine();
switch (choice) {
case 1:
System.out.println(questions.get(0));
compare(input.nextLine());
break;
case 5:
start();
break;
default:
question();
break;
}
}
private static void compare(String answer){
String array[] = new String[answers.size()];
for(int j =0;j<answers.size();j++){
array[j] = answers.get(j);
}
String compare = array[0].toString();
if(answer == compare){
System.out.println("Correct answer!");
questions.remove(0);
answers.remove(0);
} else{
System.out.println("Answer isn't correct!");
answer();
}
}