我正在使用数组制作词汇练习程序(我知道,不是列表,但我需要数组练习)。但是,一切顺利,直到程序评估类型的答案(当程序向用户询问时)是否是实际定义。我总是得到错误的答案,我不知道为什么。这是整个计划:
import java.util.Scanner;
public class organ {
public Scanner input = new Scanner(System.in);
public String language;
public int temp;
public static void main(String[] args){
organ organObject = new organ();
organObject.greeting();
organObject.ending();
}
public void greeting(){
System.out.println("Hello! Do you want to practice vocabulary in English or Spanish?");
//Need to fix this grammar.
System.out.println("!Hola! ?Quieres practicar el vocabulario en ingles o espanol?");
language = input.nextLine();
checkLanguage(language);
}
public void checkLanguage(String answer){
if (language.equals("English") || language.equals("ingles")){
englishTree();
}
else{
spanishTree();
}
}
public void englishTree(){
System.out.println("How many words do you want to practice using?");
int temp = input.nextInt();
String[] wordStorage = new String[temp];
String[] meaningStorage = new String[temp];
String answer;
for(int counter = 0; counter < wordStorage.length; counter++){
System.out.println("What is word number " + (1 + counter) + "?");
wordStorage[counter] = input.next();
System.out.println("What is def number " + (1 + counter) + "?");
meaningStorage[counter] = input.next();
}
if (wordStorage.length > 10) {
System.out.println("Stop. Now.");
}
System.out.println("Alright, let's get to the good stuff.");
for(int counter = 0; counter < wordStorage.length; counter++){
System.out.println("What is the meaning of " + wordStorage[counter] + "?");
answer = input.next();
if (answer == meaningStorage[counter]){
System.out.println("Correct!");
}
if (answer != meaningStorage[counter]){
System.out.println("Wrong. The answer is " + meaningStorage[counter] + ".");
}
}
}
public void spanishTree(){
System.out.println("Espere.");
}
public void ending(){
System.out.println("This is the ending of the program for now. Bye!");
}
} 问题出在“好吧,让我们找到好东西。”
答案 0 :(得分:1)
您正在使用==
来比较字符串。使用.equals()
str1.equals(str2)
代替str1 == str2
。