Java If语句不正确地计算字符串
编译此程序时,如果给出正确答案,每个案例中的if语句的计算结果始终为false(错误),代码如下:Java If语句不正确地计算字符串,java,string,if-statement,Java,String,If Statement,编译此程序时,如果给出正确答案,每个案例中的if语句的计算结果始终为false(错误),代码如下: import java.util.Scanner; class GameStarts { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String ret; byte qnum; String ans; String correct = "Awesom
import java.util.Scanner;
class GameStarts {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String ret;
byte qnum;
String ans;
String correct = "Awesomely correct!";
String wrong = "Darn it! Almost got it!";
System.out.println("Do you think you know your stuff?");
ret = sc.nextLine();
if (ret.equals("yes") || ret.equals("Yes"))
{
System.out.println("Well, then let's test what you know! Choose a number from 1 to 5!");
qnum = sc.nextByte();
switch (qnum)
{
case 1:
System.out.println("In what year did the French Revolution start?");
ans = sc.nextLine();
sc.nextLine();
if (ans.equals("1789") || ans.equalsIgnoreCase("seventeen eighty nine"))
{System.out.println(correct);}
else
{System.out.println(wrong);}
break;
case 2:
System.out.println("How many protons does a sodium atom have?");
ans = sc.nextLine();
sc.nextLine();
if (ans.equals("11") || ans.equalsIgnoreCase("eleven"))
{System.out.println(correct);}
else
{System.out.println(wrong);}
break;
case 3:
System.out.println("What is 2^6*0.5-12?");
ans = sc.nextLine();
sc.nextLine();
if (ans.equals("20") || ans.equalsIgnoreCase("twenty"))
{System.out.println(correct);}
else
{System.out.println(wrong);}
break;
case 4:
System.out.println("Which is the lowest numbered element in the periodic table?");
ans = sc.nextLine();
sc.nextLine();
if (ans.equalsIgnoreCase("hydrogen"))
{System.out.println(correct);}
else
{System.out.println(wrong);}
break;
case 5:
System.out.println("Which is the unit that measures Coulombs per second?");
ans = sc.nextLine();
sc.nextLine();
if (ans.equalsIgnoreCase("ampere"))
{System.out.println(correct);}
else
{System.out.println(wrong);}
break;
default:
System.out.println("Stick to the rules! 1-5!");
}
}
else
{System.out.println("Not liking that attitude, I want to hear a big yes!");}
}
}
我不确定它是否跳过了ans定义,或者我是否遗漏了什么。另外,我也非常感谢您提出的任何其他建议:)阅读行末尾可能有一个新行字符。试一试
ans.trim().equalsIgnoreCase...
修剪将消除您可能有的任何虚假空格或新行字符尝试替换
ret = sc.nextLine();
与
什么是它删除任何换行符(或制表符)并删除所有尾随和前导空格(这些东西通常会悄悄进入)
编辑:
只要在使用sc.nextLine()的所有行之后放上这样的行,如果我是对的,它应该会解决问题
编辑: 这不是问题所在。当您执行sc.readByte时,它读取2个字节-字符和换行符。当您下一次执行sc.nextLine时,它将获得剩余的换行符。解决方案:
Scanner sc = new Scanner(System.in);
String ret;
String qnum;
String ans;
String correct = "Awesomely correct!";
String wrong = "Darn it! Almost got it!";
System.out.println("Do you think you know your stuff?");
ret = sc.nextLine();
if (ret.equals("yes") || ret.equals("Yes"))
{
System.out.println("Well, then let's test what you know! Choose a number from 1 to 5!");
qnum = sc.nextLine();
switch (qnum)
{
case "1":
System.out.println("In what year did the French Revolution start?");
ans = sc.nextLine();
if (ans.equals("1789") || ans.equalsIgnoreCase("seventeen eighty nine"))
{System.out.println(correct);}
else
{System.out.println(wrong);}
break;
case "2":
System.out.println("How many protons does a sodium atom have?");
ans = sc.nextLine();
sc.nextLine();
if (ans.equals("11") || ans.equalsIgnoreCase("eleven"))
{System.out.println(correct);}
else
{System.out.println(wrong);}
break;
case "3":
System.out.println("What is 2^6*0.5-12?");
ans = sc.nextLine();
sc.nextLine();
if (ans.equals("20") || ans.equalsIgnoreCase("twenty"))
{System.out.println(correct);}
else
{System.out.println(wrong);}
break;
case "4":
System.out.println("Which is the lowest numbered element in the periodic table?");
ans = sc.nextLine();
sc.nextLine();
if (ans.equalsIgnoreCase("hydrogen"))
{System.out.println(correct);}
else
{System.out.println(wrong);}
break;
case "5":
System.out.println("Which is the unit that measures Coulombs per second?");
ans = sc.nextLine();
sc.nextLine();
if (ans.equalsIgnoreCase("ampere"))
{System.out.println(correct);}
else
{System.out.println(wrong);}
break;
default:
System.out.println("Stick to the rules! 1-5!");
}
}
else
{System.out.println("Not liking that attitude, I want to hear a big yes!");}
我在代码中所做的更改是将qnum的类型更改为字符串,将case语句更改为使用字符串,并将readByte更改为readLine。另外,我删除了第二行不需要的新行。您还使用sc.nextByte尝试读取1到5。 那不行 您必须将其作为字符串读取,然后如上所述进行修剪 在Java的早期版本中,您可以使用Integer.valueOf()将其转换为int,但从Java 7开始,您可以切换字符串:
switch(str)
{
case "1":
....
关于
sc.nextByte()代码>和ans=sc.nextLine()代码>:的可能重复项。此外,您也没有必要sc.nextLine()代码>紧跟在ans=sc.nextLine()之后代码>仍然获得相同的输出。问题是switch语句中的字符串ans,但我使用这个方法将每个ret替换为ans。是的,我想现在我将从现在开始将这些问题视为字符串。我已经从一位优秀的绅士那里找到了答案,他用同样的代码解决了以前的一个问题。他使用qnum作为int并编写:qnum=Integer.parseInt(sc.nextLine());为了简单起见,下次我将使用字符串,直到我掌握了窍门:)实际上,它在这一部分很好,因为你可以选择一个数字,它选择正确的问题。问题后来才出现
switch(str)
{
case "1":
....