0

我有點麻煩,讓我的代碼的幾個部分正常工作。 我對java還是有點新,可能會有一些方向和線索,我錯了。JAVA開關,如果其他和字符串布爾值

錯誤來自if語句。我覺得我知道他們爲什麼會犯錯,因爲||未定義,但我不知道如何解決它。我試圖讓它做的是採取輸入要麼L,R,F,B(左,右,前進和後退)。小寫輸入,並使用布爾「或」接受任一個或另一個。

import java.util.Scanner; 

公共類ChooseYourAdventure {

public static void main(String[]args) { 
    Scanner input = new Scanner(System.in);    
    System.out.print("Choose a diection: "); 
    String direction = input.nextLine().toLowerCase(); 
    System.out.printf(" %s and %s/n",getDirection (way),getYourChoice (found)); 

} 

public static String getYourChoice (String found) { 
    String result = "Unknown"; 
    switch (found) 
    { 
    case "l": 
     result = " now we all know you can turn left unlike Zoolander"; 
     break; 

    case "left": 
     result = " now we all know you can turn left unlike Zoolander"; 
     break; 

    case "r": 
     result = " you fall down a hole never to be seen again... sad."; 
     break; 

    case "right": 
     result = " you fall down a hole never to be seen again... sad."; 
     break;  

    case "f": 
     result = " YOU ARE THE KWISATZ HADERACH!!"; 
     break; 

    case "forward": 
     result = " YOU ARE THE KWISATZ HADERACH!!"; 
     break; 

    case "b": 
     result = " you are a scaredy cat but, you live to fight or runaway another day"; 
     break; 

    case "back": 
     result = " you are a scaredy cat but, you live to fight or runaway another day"; 
     break; 
    } 
    return result; 
} 

public static String getDirection(String way) { 
    String result; 
    if (way == "l" || "left") { 
     System.out.print("Your character moves left"); 
    } 

    else if (way == "r" || "right") { 
     System.out.println("You character moves right"); 
    } 

    else if (way == "f" || "forward") { 
     System.out.println("Your character moves forward"); 
    } 

    else if (way == "b" || "back") { 
     System.out.println("Your character moves forward"); 
    } 

    else { 
     System.out.println(" You cant go that way "); 
    } 

    return result; 
} 

}

回答

0

你所有的if陳述是錯誤的。當使用||&&,你需要在||的每一側指定變量way

if (way == "l" || way == "left") { 
    System.out.print("Your character moves left"); 
}