【问题标题】:Java Menu Program "X" to exit switch [duplicate]Java菜单程序“X”退出开关[重复]
【发布时间】:2022-01-15 21:21:24
【问题描述】:

我有一个 java 程序,它使用 switch 结构和循环继续,而用户输入 1 - 5 的值,如果“x”,程序退出。我一直在“choice = input.next().charAt(0);”上出错和“开关(选择){” 我对编码很陌生,所以如果这是一个愚蠢的问题,我深表歉意。完整代码如下:

import java.util.Scanner;
    public class Menu {
     public static void main(String args[]) {
Scanner sc = new Scanner(System.in); 
    char c = sc.next().charAt(0);

    while (true)  {
System.out.println("Java Help Menu \n 1. Option 1 \n 2. Option 2 \n 3. Option 3 \n 4. Option 4 \n 5. Option 5 \n x. Exit \n");        
    choice = input.next().charAt(0);
        switch (choice) {
        case '1':
            System.out.println("Selection: 1 \n");
            break;
        case '2':
            System.out.println("Selection: 2 \n");
            break;
        case '3':
            System.out.println("Selection: 3 \n");
            break;
        case '4':
            System.out.println("Selection: 4 \n");
            break;
        case '5':
            System.out.println("Selection: 5 \n");
            break;
        case 'x':
            System.out.println("Thank you. Good bye.");
        }
    } 
}

}

【问题讨论】:

  • choice 从未声明
  • 我一直出错你能告诉我们消息吗?
  • SwitchCase.java:11: 错误:找不到符号选择 = input.next().charAt(0); ^ 符号:变量选择位置:类 SwitchCase SwitchCase.java:11:错误:找不到符号选择 = input.next().charAt(0); ^ 符号:变量输入位置:类 SwitchCase SwitchCase.java:12:错误:找不到符号开关(选择){ ^ 符号:变量选择位置:类 SwitchCase SwitchCase.java:12:错误:非法括号表达式开关(选择){
  • choice = input.next().charAt(0);更改为char choice = input.next().charAt(0);
  • 我做了,现在出现错误:找不到符号“char selection = input.next().charAt(0); ^ symbol: variable input location: class SwitchCase”

标签: java loops char switch-statement exit


【解决方案1】:

您的代码中有一些错误

  1. 选择没有被宣布

  2. 你将 char 和 int 混合在一起

  3. input 必须是 sc

  4. 你在循环之前读取了一个字符,这是不需要的

    
    public class Menu {
        public static void main(String args[]) {
            Scanner sc = new Scanner(System.in);
    
            while (true) {
                System.out.println(
                        "Java Help Menu \n 1. Option 1 \n 2. Option 2 \n 3. Option 3 \n 4. Option 4 \n 5. Option 5 \n x. Exit \n");
                char choice = sc.next().charAt(0);
                switch (choice) {
                case '1':
                    System.out.println("Selection: 1 \n");
                    break;
                case '2':
                    System.out.println("Selection: 2 \n");
                    break;
                case '3':
                    System.out.println("Selection: 3 \n");
                    break;
                case '4':
                    System.out.println("Selection: 4 \n");
                    break;
                case '5':
                    System.out.println("Selection: 5 \n");
                    break;
                case 'x':
                    System.out.println("Thank you. Good bye.");
                }
            }
        }
    }
    

【讨论】:

    【解决方案2】:

    你不能在switch 语句中混合类型,编译的唯一原因是你的角色 'x' 被隐式转换为 int 的值ascii 表。因此,为了满足这个条件,用户需要输入 120 作为值,因为这是 ascii table 中小写 x 的值

    为了让这个程序按照你的预期工作,使用 String 作为输入并检查 "1" "2" ... "x"

    【讨论】:

    • 我也试过了,但仍然收到错误:找不到符号 "choice = input.next().charAt(0); switch (choice) {"
    猜你喜欢
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    相关资源
    最近更新 更多