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

为何调用错误检查方法时C#字符串未重置?控制台程序问题

问题分析与修复:无效输入后有效内容无法替换的问题

问题描述

开发控制台密码破解小游戏时,要求用户输入5位数字字符串。输入有效内容(如12345)时程序正常,但输入无效内容(如"a")后,虽然会提示重新输入,但再次输入有效数字后,最终输出的仍是最初的无效输入。

原代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ErrorCheck
{
    internal class Program
    {
        static void Main(string[] args)
        {           
            string code=enterCode();
            errorCheck(code);
            Console.WriteLine(code);
            Console.ReadLine();
        }
    
private static string enterCode()
        {
            Console.Write("Please enter code ");
            string code = Console.ReadLine();
            return code;
        }

        private static void errorCheck(string code)
        {
            bool valid = true;
            int number;
            valid = Int32.TryParse(code, out number);
            if (number > 99999) valid = false;
            if (number < 0) valid = false;
            if (valid == false)
            {
                Console.WriteLine("You did not enter a valid code");
                Console.WriteLine("Press any key to try again");
                Console.ReadLine();
                enterCode();
            }
            return;
        }
    }
}

错误原因

  1. 无返回值方法无法更新原变量:errorCheck是void类型方法,当输入无效时调用enterCode()获取了新输入,但这个新输入没有赋值给Main方法中的code变量,导致code始终是最初的无效值。
  2. 校验逻辑存在漏洞:Int32.TryParse失败时,number会被默认设为0,此时判断number < 0不会触发,但逻辑上应先确认解析成功,再判断数值范围,避免用默认值误判。

修复方案

方案1:修改errorCheck为返回有效输入

将errorCheck改为返回string类型,在无效输入时递归调用并返回新的有效内容,Main方法接收返回值更新原变量:

using System;

namespace ErrorCheck
{
    internal class Program
    {
        static void Main(string[] args)
        {           
            string code = enterCode();
            // 接收校验后的有效输入,更新原变量
            code = errorCheck(code);
            Console.WriteLine(code);
            Console.ReadLine();
        }
    
        private static string enterCode()
        {
            Console.Write("Please enter code ");
            return Console.ReadLine();
        }

        private static string errorCheck(string code)
        {
            bool valid = true;
            int number;
            // 先判断解析是否成功
            if (!Int32.TryParse(code, out number))
            {
                valid = false;
            }
            else
            {
                // 解析成功后再判断数值范围
                valid = number >= 0 && number <= 99999;
            }
            
            if (!valid)
            {
                Console.WriteLine("You did not enter a valid code");
                Console.WriteLine("Press any key to try again");
                Console.ReadLine();
                // 递归调用,返回新输入的有效内容
                return errorCheck(enterCode());
            }
            // 输入有效时返回原内容
            return code;
        }
    }
}

方案2:合并输入与校验(更健壮)

把输入和校验逻辑合并到一个方法中,用循环替代递归,避免递归可能的栈溢出问题,逻辑更清晰:

using System;

namespace ErrorCheck
{
    internal class Program
    {
        static void Main(string[] args)
        {           
            string code = GetValidCode();
            Console.WriteLine(code);
            Console.ReadLine();
        }
    
        // 循环获取输入,直到得到有效内容
        private static string GetValidCode()
        {
            while (true)
            {
                Console.Write("Please enter code ");
                string input = Console.ReadLine();
                int number;
                // 同时判断解析成功和数值范围
                if (Int32.TryParse(input, out number) && number >= 0 && number <= 99999)
                {
                    return input;
                }
                Console.WriteLine("You did not enter a valid code. Please try again.");
            }
        }
    }
}

补充:严格校验5位数字字符串(含前导零)

如果要求输入必须是长度为5的纯数字字符串(比如01234这类带前导零的内容也合法),由于Int32.TryParse会忽略前导零,需改用字符串长度和字符校验:

using System;
using System.Linq;

namespace ErrorCheck
{
    internal class Program
    {
        static void Main(string[] args)
        {           
            string code = GetValidCode();
            Console.WriteLine(code);
            Console.ReadLine();
        }
    
        private static string GetValidCode()
        {
            while (true)
            {
                Console.Write("Please enter 5-digit code ");
                string input = Console.ReadLine();
                // 判断输入不为空、长度为5且全为数字
                if (!string.IsNullOrEmpty(input) && input.Length == 5 && input.All(char.IsDigit))
                {
                    return input;
                }
                Console.WriteLine("You did not enter a valid 5-digit code. Please try again.");
            }
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 01:15:53