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

Java Swing程序错误输出求助:输入非法值时误显示Thank you

问题修复:非法输入时误显示"Thank you"的问题

问题根源

你的代码中第一个判断条件错误使用了逻辑或(||):

if (userRating2 >= MIN || userRating2 <= MAX)

这个条件对所有整数都会成立(比如输入0时,0 <= MAX为真;输入11时,11 >= MIN为真),所以不管输入是否在1-10之间,都会先弹出"Thank you",这就是问题的核心。

修复后的完整代码

把第一个判断的||改成&&,同时将两个独立的if改成if-else结构(让逻辑流程更清晰):

import javax.swing.JOptionPane;

public class LoopLab
{
    public static void main(String[] args)
    {
        int MIN = 1;
        int MAX = 10;
        String userRating = JOptionPane.showInputDialog(null, "Rate Krabby Patties on a scale from " + MIN + " to " + MAX + ". ");
        int userRating2 = Integer.parseInt(userRating);
        
        // 修正条件为逻辑与,并用if-else替代独立if
        if (userRating2 >= MIN && userRating2 <= MAX)
        {
            JOptionPane.showMessageDialog(null, "Thank you!");
        }
        else
        {
            for (int attempts = 1; attempts <= 2; attempts++)
            {
                JOptionPane.showMessageDialog(null, "Please input a number from " + MIN + " to " + MAX + ".");
                JOptionPane.showMessageDialog(null, "Please try again.");
                String userRating3 = JOptionPane.showInputDialog(null,
                    "Rate Krabby Patties on a scale from " + MIN + " to " + MAX + ". "
                );
                int userRating4 = Integer.parseInt(userRating3);
                if (userRating4 >= MIN && userRating4 <= MAX)
                {
                    JOptionPane.showMessageDialog(null, "Thank you!");
                    break;
                }
                if (attempts == 2)
                {
                    JOptionPane.showMessageDialog(null, "Come back later and try again.");
                    break;
                }
            }
        }
    }
}

额外优化建议(可选)

  • 处理非整数输入异常:用户输入字符串时,Integer.parseInt会抛出NumberFormatException,可以用try-catch块包裹避免程序崩溃。
  • 合并重复提示:把两次弹窗合并成一次,减少用户操作步骤。

示例(添加异常处理):

import javax.swing.JOptionPane;

public class LoopLab
{
    public static void main(String[] args)
    {
        int MIN = 1;
        int MAX = 10;
        boolean isValid = false;
        int userRating = -1;

        // 第一次输入,处理非数字异常
        String firstInput = JOptionPane.showInputDialog(null, "Rate Krabby Patties on a scale from " + MIN + " to " + MAX + ". ");
        try {
            userRating = Integer.parseInt(firstInput);
            if (userRating >= MIN && userRating <= MAX) {
                isValid = true;
                JOptionPane.showMessageDialog(null, "Thank you!");
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "请输入有效的数字!");
        }

        // 最多2次重试机会
        if (!isValid) {
            for (int attempts = 1; attempts <= 2; attempts++) {
                String retryInput = JOptionPane.showInputDialog(null, "Rate Krabby Patties on a scale from " + MIN + " to " + MAX + ". ");
                try {
                    userRating = Integer.parseInt(retryInput);
                    if (userRating >= MIN && userRating <= MAX) {
                        isValid = true;
                        JOptionPane.showMessageDialog(null, "Thank you!");
                        break;
                    } else {
                        JOptionPane.showMessageDialog(null, "请输入" + MIN + "到" + MAX + "之间的数字!");
                    }
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null, "请输入有效的数字!");
                }

                if (attempts == 2 && !isValid) {
                    JOptionPane.showMessageDialog(null, "Come back later and try again.");
                }
            }
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 20:30:48