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

C#乘法测验程序如何在输入校验通过后终止do-while循环

问题核心原因

你当前的循环无法终止是两个逻辑漏洞导致的:

  • 控制循环终止的end变量是Main方法的局部变量,Validate方法内修改的PlaceHolder是方法内的局部变量,二者完全独立,修改PlaceHolder不会对外部的end产生任何影响,因此循环判断条件end == 0永远成立,循环不会停止。
  • 现有输入校验逻辑存在缺陷:如果用户输入混合数字和字符的内容(比如12a),不会触发全字母判断,后续执行Int32.Parse时会直接抛出异常导致程序崩溃。

修复方案

我们可以通过调整Validate方法的返回值、优化校验逻辑来解决问题,无需额外定义全局控制变量:

  1. 将Validate方法的返回值改为bool类型,校验通过返回true,校验失败返回false
  2. 使用int.TryParse方法做格式校验,原生支持空值、非数字格式的判断,避免手动判断的遗漏,也不会抛出异常
  3. 直接用Validate方法的返回值作为循环终止条件

修改后的完整代码

using System;

namespace _17th_Nov_assignment_program
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the 10 question multiplication quiz !\n");
            Console.WriteLine("Answer each question and find out your score at the end !\n");

            //Question 1
            Console.WriteLine("Question 1.");
            // 保存题目的正确答案,后续可以用来判断用户答案是否正确
            string correctAnswer = Questions();
            // 循环校验输入,直到校验通过才退出循环
            do
            {
                // 校验通过就退出循环
            } while (!Validate());

            Console.ReadKey();
        }

        private static string Questions()
        {
            int Calc = 0;

            // This creates Random and generates value one and two randomly from 1 to 10.
            Random r = new Random();
            int Value_One = r.Next(1, 10);
            int Value_Two = r.Next(1, 10);

            // This multiplies them together
            int Multiply = Value_One * Value_Two;
            Calc = Multiply;

            // This prints the multiplication question
            Console.WriteLine("Please calculate: " + Value_One + " * " + Value_Two + "\n");
            return Calc.ToString();
        }

        private static bool Validate()
        {
            Console.WriteLine("Please enter your answer below\n");
            string usrAns = Console.ReadLine();
            const int minValue = 1;
            const int maxValue = 100;

            // 一次性判断是否是有效数字,以及数字范围是否符合要求
            if (!int.TryParse(usrAns, out int userNum) || userNum < minValue || userNum > maxValue)
            {
                Console.WriteLine("Please enter a valid number!");
                // 校验不通过,返回false,循环继续
                return false;
            }
          
            Console.WriteLine("Valid answer, now lets see if it's correct! \n");
            // 校验通过,返回true,循环终止
            return true;
        }
    }
}

补充说明

如果你需要拿到用户输入的有效答案做后续的对错判断,可以修改Validate方法的返回值为元组,同时返回校验结果和用户输入的数字,示例如下:

private static (bool isValid, int userAnswer) Validate()
{
    Console.WriteLine("Please enter your answer below\n");
    string usrAns = Console.ReadLine();
    const int minValue = 1;
    const int maxValue = 100;

    if (!int.TryParse(usrAns, out int userNum) || userNum < minValue || userNum > maxValue)
    {
        Console.WriteLine("Please enter a valid number!");
        return (false, 0);
    }
  
    Console.WriteLine("Valid answer, now lets see if it's correct! \n");
    return (true, userNum);
}

调用时直接接收返回值即可:

int userAnswer;
do
{
    var res = Validate();
    if (res.isValid)
    {
        userAnswer = res.userAnswer;
        break;
    }
} while (true);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 13:09:02