我在尝试编译我的披萨订购程序时遇到错误消息

时间:2019-10-05 04:01:16

标签: java

程序由于某种原因不希望编译。

我尝试为每个额外的浇头声明单独的浇头变量,但这只会产生比以前更多的错误。

System.out.println("Would you like pepperoni added? (Y/N): ");
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == "Y" || numberOfToppings == 'y') {
  numberOfToppings = numberOfToppings + 1;
  toppings = toppings + "and pepperoni.";
} else {
  numberOfToppings = 0;
}
//Prompt for sausage and store in numberOfToppings
System.out.println("Would you like sausage added? (Y/N): ");
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == "Y" || numberOfToppings == 'y') {
  numberOfToppings = numberOfToppings + 1;
  toppings = toppings + "and sausage.";
} else {
  numberOfToppings = 0;
}
//prompt for onion and store in numberOfToppings
System.out.println("Would you like onion added? (Y/N): ");
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == "Y" || numberOfToppings == 'y') {
  numberOfToppings = numberOfToppings + 1;
  toppings = toppings + "and onion.";
} else {
  numberOfToppings = 0;
}
//prompt for mushroom and store in numberOfToppings
System.out.println("Would you like mushroom added? (Y/N): ");
numberOfToppings = keyboard.nextLine().charAt(0);
if (numberOfToppings == "Y" || numberOfToppings == 'y') {
  numberOfToppings = numberOfToppings + 1;
  toppings = toppings + "and mushroom.";
} else {
  numberOfToppings = 0;
}

好吧,我希望它可以编译并且可以测试我的程序,但是当我尝试编译所生成的错误消息时,似乎是

PizzaOrder.java:90: error: bad operand types for binary operator '=='
    if (numberOfToppings == "Y" || numberOfToppings == 'y') {
                         ^
  first type:  int
  second type: String
PizzaOrder.java:99: error: bad operand types for binary operator '=='
    if (numberOfToppings == "Y" || numberOfToppings == 'y') {
                         ^
  first type:  int
  second type: String
PizzaOrder.java:108: error: bad operand types for binary operator '=='
    if (numberOfToppings == "Y" || numberOfToppings == 'y') {
                         ^
  first type:  int
  second type: String
PizzaOrder.java:117: error: bad operand types for binary operator '=='
    if (numberOfToppings == "Y" || numberOfToppings == 'y') {
                         ^
  first type:  int
  second type: String
4 errors

1 个答案:

答案 0 :(得分:0)

您将同一变量用于不同的事物。 因为在比较字符串时使用.equals()而不是==,所以您也不能比较不同类型的变量。 如果要将变量加1,可以执行i++,它与i = i + 1i += 1相同。 +=也适用于字符串,但是我建议使用StringBuilder。在else语句中,您将变量初始化回0而不添加0,因此添加0是多余的,因此可能什么也不做。

代码如下:

    int numberOfToppings = 0;
    String toppings = "";
    String choice;
    Scanner keyboard = new Scanner(System.in);

    System.out.println("Would you like pepperoni added? (Y/N): ");
    choice = keyboard.nextLine();
    if (choice.equals("Y") || choice.equals("y")) {
        numberOfToppings++;
        toppings += "+ pepperoni\n";
    }
//Prompt for sausage and store in numberOfToppings
        System.out.println("Would you like sausage added? (Y/N): ");
        choice = keyboard.nextLine();
        if (choice.equals("Y") || choice.equals("y")) {
            numberOfToppings++;
            toppings += "+ sausage\n";
        }
//prompt for onion and store in numberOfToppings
        System.out.println("Would you like onion added? (Y/N): ");
        choice = keyboard.nextLine();
        if (choice.equals("Y") || choice.equals("y")) {
            numberOfToppings++;
            toppings += "+ onion\n";
        }
//prompt for mushroom and store in numberOfToppings
        System.out.println("Would you like mushroom added? (Y/N): ");
        choice = keyboard.nextLine();
        if (choice.equals("Y") || choice.equals("y")) {
            numberOfToppings++;
            toppings += "+ mushroom\n";
        }

    System.out.println("Number of Toppings :  " + numberOfToppings);
    System.out.println("Toppings : \n" + toppings);