Java 没有从for循环中的if语句获得正确的输出?
我正在用数组制作一个词汇练习程序(我知道,不是列表,但我需要数组练习)。然而,在程序评估键入的答案(当程序询问用户时)是否为实际定义之前,一切都进展顺利。我总是得到错误的答案,我不知道为什么。以下是整个计划:Java 没有从for循环中的if语句获得正确的输出?,java,if-statement,for-loop,Java,If Statement,For Loop,我正在用数组制作一个词汇练习程序(我知道,不是列表,但我需要数组练习)。然而,在程序评估键入的答案(当程序询问用户时)是否为实际定义之前,一切都进展顺利。我总是得到错误的答案,我不知道为什么。以下是整个计划: import java.util.Scanner; public class organ { public Scanner input = new Scanner(System.in); public String language; public int temp; public s
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!");
}
import java.util.Scanner;
公营机关{
公共扫描仪输入=新扫描仪(System.in);
公共字符串语言;
公共内部温度;
公共静态void main(字符串[]args){
器官对象=新器官();
问候语();
organObject.ending();
}
公众问候语(){
System.out.println(“你好!你想用英语还是西班牙语练习词汇?”;
//我需要修正这个语法。
System.out.println(“!Hola!?安静地练习西班牙语?”;
language=input.nextLine();
检查语言(语言);
}
公共无效检查语言(字符串应答){
if(language.equals(“英语”)| language.equals(“英格尔”)){
英语树();
}
否则{
西班牙树();
}
}
公共英语树(){
System.out.println(“您想练习使用多少单词?”);
int temp=input.nextInt();
String[]wordStorage=新字符串[temp];
字符串[]表示存储=新字符串[temp];
字符串回答;
for(int counter=0;counter10){
System.out.println(“立即停止”);
}
System.out.println(“好的,让我们开始讨论好的东西。”);
for(int counter=0;counter
}
问题出在“好的,让我们开始讨论好的东西”下面。您正在使用
==
来比较字符串。使用.equals()
str1.equals(str2)
而不是str1==str2
谢谢!有趣的是,我实际上有另一篇与此非常相似的帖子,但仍然不记得这篇。我感谢你的帮助!