gpt4 book ai didi

java - 程序无法正确读取输入

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:49 28 4
gpt4 key购买 nike

我查找了一些类似的问题,但找不到一个答案足以帮助我解决问题的问题。

我正在尝试读取用户输入,但似乎没有将变量设置为输入。

例如:

 System.out.println("Would you like Syrup?(Y/N)");
String wantSyrup = keyboard.next();
char syrupWant = wantSyrup.charAt(0);

switch (syrupWant){

case 'y':
System.out.println("Which syrup would you like?");
System.out.println("Hot fudge, Chocolate, Caramel, Strawberry");
syrupChoice = keyboard.nextLine();
keyboard.nextLine();
System.out.println("You entered: "+syrupChoice);

if((syrupChoice.equalsIgnoreCase(hotFudge)) | (syrupChoice.equalsIgnoreCase(chocolateSyrup))|
(syrupChoice.equalsIgnoreCase(caramel))| (syrupChoice.equalsIgnoreCase(strawberrySyrup))){
System.out.println(syrupChoice);
}else{
System.out.println("Not a valid choice. You now have Hot Fudge as your syrup.");
syrupChoice = hotFudge;
}
break;
default: break;
}

是不是那个非常长的 If 语句把事情弄乱了?我从来没有在一个 if 中放入那么多 OR。

当我输入任何类型的糖浆时,控制台都会返回此信息..

Would you like Syrup?(Y/N)
y
Which syrup would you like?
Hot fudge, Chocolate, Caramel, Strawberry
caramel
You entered:
Not a valid choice. You now have Hot Fudge as your syrup.

注意:它会等待我输入一个值。这就是为什么我有

         syrupChoice = keyboard.nextLine();
keyboard.nextLine();

但无论我输入什么,它都会显示空白。我显然希望它正确读取输入并打印正确的值。 (如果数据无效,请更改为热软糖。)

编辑 ** 我现在将代码更改为:

 System.out.println("Would you like Syrup?(Y/N)");
String wantSyrup = keyboard.next();
char syrupWant = wantSyrup.charAt(0);

switch (syrupWant){

case 'y':
System.out.println("Which syrup would you like?");
System.out.println("Hot fudge, Chocolate, Caramel, Strawberry");
syrupChoice = keyboard.nextLine();
keyboard.nextLine();
System.out.println("You entered: "+syrupChoice);

if(syrupChoice.equalsIgnoreCase(hotFudge)
|| syrupChoice.equalsIgnoreCase(chocolateSyrup)
|| syrupChoice.equalsIgnoreCase(caramel)
|| syrupChoice.equalsIgnoreCase(strawberrySyrup))
{
System.out.println(syrupChoice);
}else{
System.out.println("Not a valid choice. You now have Hot Fudge as your syrup.");
syrupChoice = hotFudge;
}
break;
default: break;
}

结果还是一样

最佳答案

有一些小问题。首先,|是按位或运算符,||是逻辑或运算符。此外,您还有一些无关的 keyboard.nextLine()

System.out.println("Would you like Syrup?(Y/N)");
String wantSyrup = keyboard.nextLine();
char syrupWant = wantSyrup.charAt(0);

switch (syrupWant){
case 'y':
System.out.println("Which syrup would you like?");
System.out.print("Hot fudge, Chocolate, Caramel, Strawberry: ");
syrupChoice = keyboard.nextLine();
System.out.print("You entered: ");

if(syrupChoice.equalsIgnoreCase(hotFudge)
|| syrupChoice.equalsIgnoreCase(chocolateSyrup)
|| syrupChoice.equalsIgnoreCase(caramel)
|| syrupChoice.equalsIgnoreCase(strawberrySyrup))
{
System.out.println(syrupChoice);
}else{
System.out.println("Not a valid choice. You now have Hot Fudge as your syrup.");
syrupChoice = hotFudge;
}
break;
default: break;
}

关于java - 程序无法正确读取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29737282/

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