我正在处理一个简单的控制台菜单,我无法在 getValidChoice() 方法中获取菜单来选择除数组中的第一个字符之外的任何字符。对于“A”以外的任何字符,我都会收到“无效选择”消息。我究竟做错了什么?下面是代码:
干杯,
class Menu {
private String[] options;
public Menu(String[] options) {
this.options = options;
}
public void display() {
System.out.println("\t***** Property Sale System Menu *****\n");
System.out.println("A. " + options[1]);
System.out.println("B. " + options[2]);
System.out.println("C. " + options[3]);
System.out.println("D. " + options[4]);
System.out.println("X. " + options[0] + "\n");
System.out.println("Enter your selection");
}
public char getValidChoice() {
Scanner input = new Scanner(System.in);
char choice;
choice = input.nextLine().charAt(0);
char[] menuChoices = {'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'X', 'x'};
for(int i = 0; i < menuChoices.length; i++) {
while(choice != menuChoices[i]) {
System.out.println("Invalid Selection: " + choice + "\n");
display();
choice = input.nextLine().charAt(0);
}
}
return choice;
}
}