2017-07-18 74 views
-4

對於這個大膽的問題,您好,非常抱歉,但我不知道如何解決我在代碼行24和30上的需求。也即時獲得第33和第22行的錯誤消息,所以如果你能解決這些問題,那將是非常棒的。無法在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?"); 
     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; 

     } 


    } 

} 
+1

請發佈一個MCCVE(如[MCVE],但可編譯)。一些閱讀:https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#equalsIgnoreCase-java.lang.String- – 2017-07-18 11:32:15

+0

你嘗試了一個'sys.out'並檢查。用戶輸入可能需要'trim'。 – Mritunjay

+0

你不應該一次檢查'equalsIgnoreCase'。 – soorapadman

回答

0

首先在主要方法中,您不能使用public static。聽起來像restOrEat應該是一個是或否的字符串,所以你不能說nextInt()。如果只有一個字,則使用next();如果需要整行,則使用nextLine()。並且在if語句中,您不能在equalsIgnoreCase()呼叫中使用||運營商,但需要製作兩個並將||置於中間。

試試這個,如果它符合你的需要。

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(); 

     } 


    } 

} 
+0

你會麻煩解釋它是什麼嗎? – Mritunjay

+0

你還沒有解釋是什麼原因造成的問題? – soorapadman

1

||運算符不能應用於java.util.String。

restOrEat.equalsIgnoreCase(Eat || ToEat); 

的兩個變量EatToEat是從該類對象java.util.String。這就是爲什麼在這種情況下||運營商不起作用。

將其更改爲:

restOrEat.equalsIgnoreCase(Eat) || restOrEat.equalsIgnoreCase(ToEat); 

這將工作。

0

你不能在主要方法內寫入'public static'。

if (restOrEat.equalsIgnoreCase(TheRestroom) || restOrEat.equalsIgnoreCase(Restroom)) { 

與上述參考

0

首先把if語句是不正確的,他們應該是這樣的if (restOrEat.equalsIgnoreCase(TheRestroom) || restOrEat.equalsIgnoreCase(Restroom)) {

其次,你不能有公共和替換你都行方法內變量的靜態關鍵字。

因此改變public static restOrEat(你還缺少對象定義)int restOrEat(貌似你已經將它定義爲在類的字符串)和NextInt應該是nextInt()

還檢查了這一點Java Naming Conventions

0

你有幾個問題:

  • 或者條件(||)應該是檢查之間

    if (restOrEat.equalsIgnoreCase(TheRestroom) || restOrEat.equalsIgnoreCase(Restroom)) { } 
    
  • 您定義restOrEat爲類變量,但它重新定義(在某種程度上)在main

    public static restOrEat = userInput.NextInt(); 
    
  • public static不是方法變量有效的語法(甚至不另發),而你沒有有類型。

  • userInput.NextInt();將返回intrestOrEatString。改爲使用userInput.next()

0
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(); 

    } 
} 
} 

這裏是你的問題的解決方案。