You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java控制台饮品制作程序数字映射错误问题求助

问题分析与解决方案

核心问题根源

  • 尺寸选择逻辑混乱:获取用户尺寸选择时,只调用了input.nextInt()却没把结果存到新变量里,后续判断仍用外层的choice变量(此时choice是1,因为你是在choice == 1的分支里),所以不管用户输入2还是其他数字,都会触发choice == 1的判断,把尺寸硬设成Demi。
  • 饮品类型选择双重错误:
    1. 同样没把用户的类型选择存到新变量,判断时还是用外层的choice(此时是2),永远只会触发choice == 2的分支,把类型设成Tea。
    2. 用input.nextLine()之前,前面的input.nextInt()会留下换行符在输入缓冲区,导致input.nextLine()直接读到空字符串,根本拿不到用户输入。
  • 额外bug:尺寸选择里的合法性判断if(choice < 0 || choice > 6)完全没用,因为此时choice固定是1,永远不会触发。

修复后的完整代码

public void makeDrink(Scanner input) {
    showOptions();
    int choice = input.nextInt();
    input.nextLine(); // 清除nextInt残留的换行符

    while (choice != 0) {
        if (choice > 4 || choice < 0) {
            System.out.println("You made a invalid choice, please reset");
        }

        /*
         * 选择饮品尺寸
         */ 
        if (choice == 1) {
            System.out.println("\n\n\n\n");
            System.out.println("Choose the size");
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            System.out.println("|1. Demi (3 oz)|2. Short (8 oz)|3. Tall (12 oz)|4. Grande (16 oz)|5. Venti (20 oz)|6. Trenta (30 oz)|");
            System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
            
            // 把用户输入的尺寸选择存到独立变量里
            int sizeChoice = input.nextInt();
            input.nextLine(); // 清除换行符

            // 合法性判断改用实际的尺寸选择值
            if(sizeChoice < 1 || sizeChoice > 6) {
                System.out.println("You made a invalid choice, please try again"); 
            } else {
                // 根据用户选择设置对应尺寸
                if (sizeChoice == 1) {
                    setSize("Demi");
                } else if (sizeChoice == 2) {
                    setSize("Short");
                } else if (sizeChoice == 3) {
                    setSize("Tall");
                } else if (sizeChoice == 4) {
                    setSize("Grande");
                } else if (sizeChoice == 5) {
                    setSize("Venti");
                } else if (sizeChoice == 6) {
                    setSize("Trenta");
                }
            }
        }

        /*
         * 选择饮品类型
         */
        if (choice == 2) {
            System.out.println("\n\n\n\n");
            System.out.println("Choose the Drink");
            System.out.println("~~~~~~~~~~~~~~~~~~");
            System.out.println("|1. Coffee|2. Tea|");
            System.out.println("~~~~~~~~~~~~~~~~~~");
            
            // 用nextInt获取类型选择,避免换行符问题
            int typeChoice = input.nextInt();
            input.nextLine(); // 清除换行符

            if(typeChoice < 1 || typeChoice > 2) {
                System.out.println("You made a invalid choice, please try again");
            } else {
                // 根据用户选择设置对应类型
                if (typeChoice == 1) {
                    setType("Coffee");
                } else if (typeChoice == 2) {
                    setType("Tea");
                }
            }
        }
        
        showOptions();
        choice = input.nextInt();
        input.nextLine(); // 清除换行符
    }
}

关键修复说明

  • 为尺寸、类型选择分别创建独立变量(sizeChoice、typeChoice),彻底避免和外层的主菜单选择变量choice混淆。
  • 每次调用input.nextInt()后都用input.nextLine()清空输入缓冲区的换行符,解决后续读取时的空值异常。
  • 修正合法性判断的变量,确保判断的是用户实际输入的选择值。
  • 用else if替代多个独立if,逻辑更严谨,减少不必要的判断步骤。

内容的提问来源于stack exchange,提问作者Clayton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 04:20:58