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

C#异常处理问题:输入合法数字程序直接退出,求解决方案

问题分析与修复

你的问题核心是计算和输出逻辑被嵌套在最后一个catch块里——只有当四个输入全部抛出FormatException时,才会执行计算和打印结果的代码。如果输入合法,对应的try块执行完成后,程序就直接跳出所有try-catch结构,Main方法结束,自然就退出了,看不到任何输出。

修复后的代码

把计算和输出代码移到所有输入处理的外面,确保不管输入是否合法,都能执行这部分逻辑:

using System;

namespace MealInputLayton
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal mealOne = 0;
            decimal mealTwo = 0;
            decimal dessertOne = 0;
            decimal dessertTwo = 0;
            decimal subtotal = 0;
            const decimal TIP_PERCENTAGE = 0.2m;
            const decimal TAX_RATE = 0.07m;
            decimal total = 0;
            decimal numPeople = 2;
            decimal totalPerPerson = 0;
            decimal tip = 0;
            decimal tax = 0;

            // 处理第一个餐费输入
            Console.WriteLine("How much did your first meal cost?");
            try
            {
                mealOne = Convert.ToDecimal(Console.ReadLine());
            }
            catch (System.FormatException)
            {
                Console.WriteLine("Error for 1st meal price. Input string was not in a correct format.");
            }

            // 处理第二个餐费输入
            Console.WriteLine("How much did your second meal cost?");
            try
            {
                mealTwo = Convert.ToDecimal(Console.ReadLine());
            }
            catch (System.FormatException)
            {
                Console.WriteLine("Error for 2nd meal price. Input string was not in a correct format.");
            }

            // 处理第一个甜点输入
            Console.WriteLine("How much did your first dessert cost?");
            try
            {
                dessertOne = Convert.ToDecimal(Console.ReadLine());
            }
            catch (System.FormatException)
            {
                Console.WriteLine("Error for 1st dessert price. Input string was not in a correct format.");
            }

            // 处理第二个甜点输入
            Console.WriteLine("How much did your second dessert cost?");
            try
            {
                dessertTwo = Convert.ToDecimal(Console.ReadLine());
            }
            catch (System.FormatException)
            {
                Console.WriteLine("Error for 2nd dessert price. Input string was not in a correct format.");
            }

            // 计算与输出逻辑移到这里,确保一定会执行
            subtotal = mealOne + mealTwo + dessertOne + dessertTwo;
            tip = subtotal * TIP_PERCENTAGE;
            tax = subtotal * TAX_RATE;
            total = subtotal + tax + tip;
            totalPerPerson = total / numPeople;
            Console.WriteLine("Subtotal: " + "$" + "{0:0.00}", subtotal);
            Console.WriteLine("Tax: " + "$" + "{0:0.00}", tax);
            Console.WriteLine("Tip: " + "$" + "{0:0.00}", tip);
            Console.WriteLine("Total: " + "$" + "{0:0.00}", total);
            Console.WriteLine("Per Person: " + "$" + "{0:0.00}", totalPerPerson);

            // 可选:让程序暂停,避免直接退出看不到结果
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

额外优化建议

  • 用decimal.TryParse代替try-catch,代码更简洁,性能也更好:
    Console.WriteLine("How much did your first meal cost?");
    if (!decimal.TryParse(Console.ReadLine(), out mealOne))
    {
        Console.WriteLine("Error for 1st meal price. Input string was not in a correct format.");
    }
    
  • 避免嵌套try-catch,把每个输入的处理拆成独立块,代码可读性更高。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 21:45:35