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

Java如何实现用户直接输入Yes/No的布尔值处理及无效输入校验?

小学生数学题程序优化问题

原始代码

package hw;
import java.util.Scanner;
import java.util.Random;

public class hw8 {
    public static void main(String[] args) {
        int min = 0;
        int max = 9;
        boolean continueinput = true;
        while (continueinput == true) { //user can use the program without having to run it again.
            int number1 = (int)Math.floor(Math.random() * (max - min + 1) + min);
            int number2 = (int)Math.floor(Math.random() * (max - min + 1) + min);
            Scanner input = new Scanner(System.in);
            System.out.println(number1+" + "+number2+" = ?");
            int studentanswer = input.nextInt();
            int correctanswer = number1 + number2;
            if (studentanswer == correctanswer) {
                System.out.println("Congratulations, you solved it correctly!");
            } else {
                System.out.println("Oh, "+studentanswer+" is not correct, correct answer was "+correctanswer+". But don't worry! I'm sure you'll get it right next time!");
            }
            System.out.println("Do you want to continue solving problems? true(Yes)/false(No) (example: enter false for No)");
            continueinput = input.nextBoolean();
            if (continueinput == false) {
                System.out.println("So you're tired! That's completely okay but don't be a stranger!");
            }
        }
    }
}

问题描述

我正在开发一款面向小学生的数学题练习程序,目前用true对应“是”、false对应“否”让用户选择是否继续,但这种方式对小学生不够友好。请问如何修改代码,让用户可以直接输入“Yes”或“No”来操作?另外,如果用户输入了非指定内容,怎么提示输入无效并要求重新输入?我在布尔值处理这块有点搞不懂,希望能得到解答。


修改方案及代码

核心修改思路

  • 把布尔值输入替换为字符串输入,直接接收用户的“Yes/No”(支持大小写变体)
  • 增加输入校验循环,确保只有合法输入才会执行后续逻辑
  • 处理输入缓冲区残留的换行符,避免读取空字符串的问题

修改后的完整代码

package hw;
import java.util.Scanner;
import java.util.Random;

public class hw8 {
    public static void main(String[] args) {
        int min = 0;
        int max = 9;
        boolean continueinput = true;
        Scanner input = new Scanner(System.in); //  Scanner移到循环外,避免重复创建
        
        while (continueinput) { // 简化布尔判断,直接使用变量本身
            int number1 = (int)Math.floor(Math.random() * (max - min + 1) + min);
            int number2 = (int)Math.floor(Math.random() * (max - min + 1) + min);
            
            System.out.println(number1 + " + " + number2 + " = ?");
            int studentanswer = input.nextInt();
            int correctanswer = number1 + number2;
            
            if (studentanswer == correctanswer) {
                System.out.println("恭喜你,答对啦!");
            } else {
                System.out.println("哦," + studentanswer + "不对哦,正确答案是" + correctanswer + "。别担心,下次肯定能答对!");
            }
            
            input.nextLine(); // 清空缓冲区的换行符,避免nextLine()读取空内容
            
            // 循环校验继续选择的输入,直到用户输入合法内容
            while (true) {
                System.out.println("还要继续做题吗?请输入Yes(是)或No(否)");
                String choice = input.nextLine().trim(); // 去除输入前后的空格
                
                if (choice.equalsIgnoreCase("Yes")) {
                    continueinput = true;
                    break; // 退出输入校验循环
                } else if (choice.equalsIgnoreCase("No")) {
                    continueinput = false;
                    System.out.println("你累啦!没关系,随时再来哦!");
                    break;
                } else {
                    System.out.println("输入无效,请输入Yes或No!");
                }
            }
        }
        input.close(); // 关闭Scanner资源
    }
}

关键修改说明

  1. Scanner复用:把Scanner声明移到循环外,避免每次循环创建新对象,提升效率
  2. 缓冲区处理:input.nextInt()后调用input.nextLine(),清除残留的换行符,避免后续nextLine()读取空字符串
  3. 输入校验循环:嵌套while(true)循环,只要用户输入不合法就重复提示,直到输入正确内容才退出
  4. 大小写兼容:使用equalsIgnoreCase()方法,支持用户输入yes、YES、no、NO等多种格式,更友好
  5. 简化布尔判断:while (continueinput)比while (continueinput == true)更简洁,符合Java编码习惯

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 22:52:43