Java 未能在if语句中使用字符串
您好,很抱歉这个大胆的问题,但我不知道如何解决我在代码第24行和第30行的需求。另外,我在第33行和第22行收到错误消息,所以如果你能解决这些问题,那就太棒了Java 未能在if语句中使用字符串,java,string,if-statement,user-input,Java,String,If Statement,User Input,您好,很抱歉这个大胆的问题,但我不知道如何解决我在代码第24行和第30行的需求。另外,我在第33行和第22行收到错误消息,所以如果你能解决这些问题,那就太棒了 import java.util.Scanner; public class CheckOrder { public static Scanner userInput = new Scanner(System.in); public static Scanner userInputStarters = new Sca
import java.util.Scanner;
public class CheckOrder {
public static Scanner userInput = new Scanner(System.in);
public static Scanner userInputStarters = new Scanner(System.in);
public static String menu;
public static String check;
public static String TheRestroom = "The Restroom";
public static String Restroom = "Restroom";
public static String Eat = "Eat";
public static String ToEat = "To Eat";
public static String restOrEat;
public static void main(String[] args) {
System.out.print("Hello and Welcome to the SOMETHING restaurant. Would you like to eat or use the restroom?");
public static restOrEat = userInput.NextInt();
if (restOrEat.equalsIgnoreCase(TheRestroom || Restroom)) {
System.out.println("Please go ahead to the restroom, it's over there.");
System.exit(1);
}
if (restOrEat.equalsIgnoreCase(Eat || ToEat)) {
System.out.print("What would you like to eat for starters? Press 1 for Cheese & Bacon, 2 for Sald (With options) and 3 for noodles");
public static String starter = userInputStarters;
}
}
}
首先,在main方法中,不能使用publicstatic。而且restOrEat听起来应该是一个yes或no字符串,所以你不能说nextInt。如果只是一个单词,请使用next;如果需要整行,请使用nextLine。在IF语句中,你不能在均衡器调用中使用运算算子,但是你需要把两个运算放在中间,把π放在中间。 如果它符合你的需要,试试这个
import java.util.Scanner;
public class CheckOrder {
public static Scanner userInput = new Scanner(System.in);
public static Scanner userInputStarters = new Scanner(System.in);
public static String menu;
public static String check;
public static String TheRestroom = "The Restroom";
public static String Restroom = "Restroom";
public static String Eat = "Eat";
public static String ToEat = "To Eat";
public static String restOrEat;
public static void main(String[] args) {
System.out.print("Hello and Welcome to the SOMETHING restaurant. Would you like to eat or use the restroom?");
restOrEat = userInput.next();
if (restOrEat.equalsIgnoreCase(TheRestroom) || restOrEat.equalsIgnoreCase(Restroom)) {
System.out.println("Please go ahead to the restroom, it's over there.");
System.exit(1);
}
if (restOrEat.equalsIgnoreCase(Eat) || restOrEat.equalsIgnoreCase(ToEat)) {
System.out.print("What would you like to eat for starters? Press 1 for Cheese & Bacon, 2 for Sald (With options) and 3 for noodles");
int starter = userInputStarters.nextInt();
}
}
}
| |运算符不能应用于java.util.String
restOrEat.equalsIgnoreCase(Eat || ToEat);
变量Eat和ToEat都是类java.util.String中的对象。这就是| |运算符在这种情况下不起作用的原因
将其更改为:
restOrEat.equalsIgnoreCase(Eat) || restOrEat.equalsIgnoreCase(ToEat);
这将起作用。您不能像在main方法中那样编写“publicstatic”
if (restOrEat.equalsIgnoreCase(TheRestroom) || restOrEat.equalsIgnoreCase(Restroom)) {
用上述引用替换两行首先,如果if语句不正确,则它们应该是这样的if restOrEat.equalsignorecasetrestorom | restOrEat.equalsignorecase restoroom{ 其次,在方法中的变量前面不能有公共关键字和静态关键字 因此,将公共静态restOrEat更改为int restOrEat也会丢失对象定义,似乎您已经将其定义为类中的字符串,并且NextInt应该是NextInt
另外,请检查此项您有几个问题: 或条件| |应介于到检查之间
if (restOrEat.equalsIgnoreCase(TheRestroom) || restOrEat.equalsIgnoreCase(Restroom)) { }
您将restOrEat定义为类变量,但在main中重新定义了它
publicstatic对于方法变量(甚至不是单独的)语法无效,并且您没有该类型
userInput.NextInt;将返回int,但restOrEat是字符串。请改用userInput.next
以下是您问题的解决方案。请发布一个类似于但可编译的MCCVE。阅读内容:您是否尝试将sys.out放入并检查。用户输入可能需要修剪。您不应该一次性检查equalsIgnoreCase。您认为TheRestroom | | Restroom做什么?您是否愿意解释它是什么?您仍然没有解释什么这不是问题的起因吗?
public static restOrEat = userInput.NextInt();
import java.util.Scanner;
class CheckOrder {
public static Scanner userInput = new Scanner(System.in);
public static Scanner userInputStarters = new Scanner(System.in);
public static String menu;
public static String check;
public static String TheRestroom = "The Restroom";
public static String Restroom = "Restroom";
public static String Eat = "Eat";
public static String ToEat = "To Eat";
public static String restOrEat;
public static void main(String[] args) {
System.out.print("Hello and Welcome to the SOMETHING restaurant. Would you like to eat or use the restroom?");
restOrEat = userInput.next();
if (restOrEat.equalsIgnoreCase(TheRestroom)||restOrEat.equalsIgnoreCase(Restroom)) {
System.out.println("Please go ahead to the restroom, it's over there.");
System.exit(1);
}
if (restOrEat.equalsIgnoreCase(Eat)||restOrEat.equalsIgnoreCase(ToEat)) {
System.out.print("What would you like to eat for starters? Press 1 for Cheese & Bacon, 2 for Sald (With options) and 3 for noodles");
int starter = userInputStarters.nextInt();
}
}
}