我还是 Java 新手。我正在尝试创建一个程序,用户必须在其中回答多项选择测验。用户将输入他们的答案,这些输入将形成一个数组。然后我计划使用 for 循环将用户的答案数组与正确答案数组进行比较,以告诉用户它们是对还是错。
但是,我似乎没有正确比较 if 语句中的 2 个数组。每次我运行程序时,它都会直接进入 else 语句。
我的预感是扫描仪类实际上并没有存储值?
任何人都可以帮忙吗?
部分代码如下:
//Above this section is just a collection of "System.out.println" statements that state questions and answers the user choose from.
int x;
String answers [] = {"a", "a", "b"};
//answers array has the correct answer
Scanner in = new Scanner(System.in);
String answerEntered [] = new String [5];
//user input will be in this arra
for(x=0 ; x<3 ; x++)
{
System.out.print((1+x)+". ");
answerEntered[x] = in.nextLine();
}
for( x=0; x<3; x++)
{
**if(answerEntered[x] == answers[x])
{
System.out.println("For Question "+(x+1)+", you are Correct!");
}**
//This if section does not seem to work. Every time i run the code it automatically goes to the else statement.
else
{
System.out.println("The correct answer for Question "+(x+1)+" is: "+answers[x]);
}
}