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

两数求和程序连续输入数字的问题求助

两数求和程序连续输入数字的问题求助

Hey,我看了你的代码,问题出在这几个关键地方:

  • 你只在循环外面调用了一次Console.ReadLine(),所以enter变量自始至终都是第一次输入的内容,x和y自然会是同一个数字
  • 你的循环逻辑有点混乱,while(true)的写法没用到点子上,而且判断条件的顺序也不对,应该先处理空输入退出的情况,再处理数字转换

我帮你调整了代码,完全贴合你的需求:

namespace test;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Enter numbers to calculate or tap ENTER to exit");
        
        // 读取第一个数字输入
        string firstInput = Console.ReadLine();
        // 先判断是否直接退出
        if (string.IsNullOrEmpty(firstInput))
        {
            return;
        }
        
        // 用TryParse避免输入非数字时程序崩溃
        if (!int.TryParse(firstInput, out int x))
        {
            Console.WriteLine("Invalid number for first input");
            return;
        }
        
        // 读取第二个数字输入
        string secondInput = Console.ReadLine();
        if (string.IsNullOrEmpty(secondInput))
        {
            return;
        }
        
        if (!int.TryParse(secondInput, out int y))
        {
            Console.WriteLine("Invalid number for second input");
            return;
        }
        
        // 按你的需求判断两个数是否都为正数
        if (x > 0 && y > 0)
        {
            int z = x + y;
            Console.WriteLine($"Sum of {x} and {y} is: {z}");
        }
        else
        {
            Console.WriteLine("Both numbers must be greater than 0");
        }
    }
}

给你解释下改动点:

  • 分两次调用Console.ReadLine(),分别获取第一个和第二个数字,彻底解决了重复用同一个输入的问题
  • 用int.TryParse替代Convert.ToInt32,这样用户输入非数字内容时,程序不会直接崩溃,还能给出友好提示
  • 调整了逻辑顺序:先处理空输入退出的情况,再验证数字格式,最后判断正数条件并求和
  • 去掉了没必要的while(true)循环,因为你的需求是输入两个数就停止,完全不需要循环

如果你想要程序可以重复计算(输入一次两个数求和后,还能继续输入新的数字对),可以把整个逻辑包在循环里,比如:

namespace test;

class Program
{
    static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine("\nEnter first number (or tap ENTER to exit):");
            string firstInput = Console.ReadLine();
            if (string.IsNullOrEmpty(firstInput))
            {
                Console.WriteLine("Exiting program...");
                break;
            }
            
            if (!int.TryParse(firstInput, out int x))
            {
                Console.WriteLine("Invalid number, please try again");
                continue;
            }
            
            Console.WriteLine("Enter second number:");
            string secondInput = Console.ReadLine();
            if (string.IsNullOrEmpty(secondInput))
            {
                Console.WriteLine("Exiting program...");
                break;
            }
            
            if (!int.TryParse(secondInput, out int y))
            {
                Console.WriteLine("Invalid number, please try again");
                continue;
            }
            
            if (x > 0 && y > 0)
            {
                int z = x + y;
                Console.WriteLine($"Sum: {z}");
            }
            else
            {
                Console.WriteLine("Both numbers need to be positive!");
            }
        }
    }
}

这样程序会一直运行,直到用户在输入第一个数字时按回车退出。

备注:内容来源于stack exchange,提问作者Learner

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 07:04:14