當我輸入牛肉,豬肉,雞肉或芝士漢堡時,應該說「這是你的」,然後輸入任何東西,同時過濾出不在數組中的任何東西。它不工作。任何幫助? (是的,我是Java新手)盡我所能從反饋中得到最好的解決,但仍然無法正常工作。當我從我的數組中輸入東西進入掃描儀時,它沒有執行正確的功能
import java.util.Scanner;
public class scannerReview {
public static void main(String[] args){
String[] array = new String[]{"Beef","Pork","Chicken","Cheeseburger"};
Scanner sc = new Scanner(System.in);
System.out.println("What do you want for dinner? Your options are Beef, Pork, Chicken, and Cheesebugers. ");
String food = sc.nextLine();
if(!food.equals(array[0])){
System.out.println("You cannot order that!");
}
else if(!food.equals(array[1])){
System.out.println("You cannot order that!");
}
else if(!food.equals(array[2])){
System.out.println("You cannot order that!");
}
else if(!food.equals(array[3])){
System.out.println("You cannot order that!");
}
else{
System.out.println("Here is your" + food);
}
}
}
控制檯:
What do you want for dinner? Your options are Beef, Pork, Chicken, and Cheesebugers.
Beef(My input)
You cannot order that!
您有一個邏輯問題。你永遠不會達到最後的其他部分。重新思考你的邏輯。 –
謝謝,修復它。當然,所有其他建議。 – Durnehviir