gpt4 book ai didi

java - If 语句错误地评估字符串

转载 作者:行者123 更新时间:2023-12-02 05:55:23 25 4
gpt4 key购买 nike

编译此程序时,在给出正确答案时,每个 case 内的 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 = "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 定义或者我是否遗漏了某些内容。另外,如果有其他建议,我将不胜感激:)

最佳答案

尝试替换

ret = sc.nextLine();

ret = sc.nextLine();
ret = ret.replace("\n","").replace("\t","").trim();

它会删除任何换行符(或制表符)并删除所有尾随和前导空格(这些东西经常会出现)是什么

编辑:

只需使用 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 的类型更改为 String,更改了 case 语句以使用字符串,并将 readByte 更改为 readLine。另外,我删除了第二个不需要的新行。

关于java - If 语句错误地评估字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23143211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com