用户输入不是调用方法(Java)
当我运行程序并将用户输入输入为(A-F)时,程序不会执行任何操作。我试图弄明白为什么当我输入字母A到Z时,程序不调用这个方法。如果我输入其中一个数字,它们将运行得非常好,并调用这些方法,但由于某些原因,这些字母给我带来了问题用户输入不是调用方法(Java),java,if-statement,methods,user-input,Java,If Statement,Methods,User Input,当我运行程序并将用户输入输入为(A-F)时,程序不会执行任何操作。我试图弄明白为什么当我输入字母A到Z时,程序不调用这个方法。如果我输入其中一个数字,它们将运行得非常好,并调用这些方法,但由于某些原因,这些字母给我带来了问题 public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String userInput; initializeShipList();
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String userInput;
initializeShipList(); // initial ships
initializeCruiseList(); // initial cruises
initializePassengerList(); // initial passengers
// add loop and code here that accepts and validates user input
// and takes the appropriate action. include appropriate
// user feedback and redisplay the menu as needed
do {
displayMenu();
userInput = scnr.nextLine();
if(userInput.equals("1")) {
addShip();
}
if(userInput.equals("2")) {
editShip();
}
if(userInput.equals("3")) {
addCruise();
}
if(userInput.equals("4")) {
editCruise();
}
if(userInput.equals("5")) {
addPassenger();
}
if(userInput.equals("6")) {
editPassenger();
}
if(userInput.toUpperCase().equals("A")) {
printShipList("active");
}
if(userInput.toUpperCase().equals("B")) {
printShipList("name");
}
if(userInput.toUpperCase().equals("C")) {
printShipList("full");
}
if(userInput.toUpperCase().equals("D")) {
printCruiseList("list");
}
if(userInput.toUpperCase().equals("E")) {
printCruiseList("details");
}
if(userInput.toUpperCase().equals("F")) {
printPassengerList();
}
}while(userInput != "x");
}
您确定printShipList和printCruiseList函数的代码正确吗?我看不出代码中有什么错误。为什么不打印userInput的值以便检查它呢。我会像System.out.println(“userInput=“+userInput+”)一样打印它;我认为是这样。当我第一次开始这个项目的时候,他们就得到了代码。它们也没有显示错误。@JamesCSguy只需在控制台中为if(userInput.toUpperCase().equals(“a”)打印一个字符串即可。。。。将输入作为。。。查看是否打印该字符串。您发布的代码不是一个字符串。尽管如此,在你发布的代码中,我没有看到初始化
userInput
。