Java 寻求回路中案例切换的建议。我的描述如下

Java 寻求回路中案例切换的建议。我的描述如下,java,Java,如何将二进制输出为十进制:选项:1二进制数:2222无效二进制数 如果用户键入高于1的二进制文件 另外,除非用户输入了错误的选项,否则如何使开关盒的默认值不显示。示例:他们输入菜单屏幕上没有的“HOOPdoop”,默认输出表示他们必须使用正确的输入重试。但是,我不希望在计算后用户输入“八进制到十进制”时输出默认值 这是我的密码: /** * @param args the command line arguments */ public static void

如何将二进制输出为十进制:选项:1二进制数:2222无效二进制数

如果用户键入高于1的二进制文件

另外,除非用户输入了错误的选项,否则如何使开关盒的默认值不显示。示例:他们输入菜单屏幕上没有的“HOOPdoop”,默认输出表示他们必须使用正确的输入重试。但是,我不希望在计算后用户输入“八进制到十进制”时输出默认值

这是我的密码:

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //VARIABLES BEFOR LOOP / FOR LOOP
        boolean Loop = true;
        String Choice;
        //OBJECTS
        Scanner input = new Scanner(System.in);
        while (Loop == true) {
            //MENU
            System.out.println("This program will convert...\n" + " \n" + "Binary to Decimal\n" + "Octal to Decimal\n" + "Decimal to Binary\n" + "Decimal to Octal\n" + "or you may Exit");
            //ASK
            System.out.println("--------------------------------------------");
            System.out.println("Which converter would you like to use? or would you like to exit? : ");
            Choice = input.nextLine();
            //VARIABLES INSIDE LOOP
            String binary = "";
            int decimal;
            String octal;
            switch (Choice) {
                case "Binary to Decimal":
                    System.out.print("Enter a binary number: ");
                    binary = input.nextLine();
                    if (binary.equals(2)) {
                        System.out.println("INVALID.");
                    } else {
                        System.out.println("Decimal: " + Integer.parseInt(binary, 2));
                        System.out.println("-----------------------------------------");
                    }
                    break;
                case "Decimal to Binary":
                    System.out.print("Enter decimal number: ");
                    decimal = input.nextInt();
                    int answer = decimal;
                    while (answer > 0) {
                        binary = (answer % 2) + binary;
                        answer = answer / 2;
                    }
                    System.out.println("The binary number = " + binary);
                    System.out.println("-----------------------------------------");
                    break;
                case "Decimal to Octal":
                    System.out.print("Enter decimal number: ");
                    decimal = input.nextInt();
                    octal = Integer.toOctalString(decimal);
                    System.out.println("Octal number = " + octal);
                    System.out.println("-----------------------------------------");
                    break;
                case "Octal to Decimal":
                    System.out.print("Enter a Octal number: ");
                    octal = input.nextLine();
                    System.out.println("Decimal: " + Integer.parseInt(octal, 8));
                    System.out.println("-----------------------------------------");
                    break;
                case "Exit":
                    System.out.println("Goodbye!!");
                    Loop = false;
                    break;
            }
        }
    }
}``` 

注意:不能将字符串与数字进行比较。将
二进制
2
进行比较将始终返回
false
。必须使用
String
操作或正则表达式来检查字符串是否为无效二进制-或使用
parseInt
引发的异常。旁注:不要让变量名以大写开头(
循环
选择
)。它们应该以小写字母开头,以避免与类名(以大写字母开头)混淆。@realponsign我是计算机科学新手,所以我真的不知道你的意思。你能给我举个例子吗?非常感谢。(: