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

如何在C#中访问作用域外变量?(禁止使用类和方法)

问题描述

我为这份作业忙活了两周,还是没解决问题。我知道可以用类和方法处理,但作业要求不能用,因为我们还没学相关内容。以下是我的C#代码,我需要累加销售总额,但当前switch分支里定义的dblTotalSales在退出分支时访问不了,也没法正确累加,该怎么解决?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
namespace Assignment_2
{
    class Program
    {
        static void Main(string[] args)
        {

            Write("Enter a salesperson's name: ");
            string name = ReadLine();

            do
            {
                Write("\n" + "Enter an item number between 1 and 4 or -1 to quit: ");
                String item = ReadLine();
                int intItem = Convert.ToInt32(item);

                Write("\n" + "Enter the quantity sold: ");
                String quantity = ReadLine();
                int intQuantity = Convert.ToInt32(quantity);

                switch (intItem)
                {
                    case 1:
                        {
                            Double price = 239.99;
                            double total = price * intQuantity;
                            WriteLine("Salesperson " + name + " sold " + intQuantity + " of item #" + intItem + " at $" + total);
                            double dblTotalSales = +total;
                            break;
                        }
                    case 2:
                        {
                            Double price = 129.75;
                            double total = price * intQuantity;
                            WriteLine("Salesperson " + name + " sold " + intQuantity + " of item #" + intItem + " at $" + total);
                            double dblTotalSales = +total;
                            break;
                        }
                    case 3:
                        {
                            Double price = 99.95;
                            double total = price * intQuantity;
                            WriteLine("Salesperson " + name + " sold " + intQuantity + " of item #" + intItem + " at $" + total);
                            double dblTotalSales = +total;
                            break;
                        }
                    case 4:
                        {
                            Double price = 350.89;
                            double total = price * intQuantity;
                            WriteLine("Salesperson " + name + " sold " + intQuantity + " of item #" + intItem + " at $" + total);
                            double dblTotalSales = +total;
                            break;
                        }
                    case -1:
                        {
                            WriteLine("Salesperson " + name + " sold a total of $" + dblTotalSales);
                            break;
                        }
                }

            } while (true);  

        }
    }
}
解决方法

核心问题分析

  1. 变量作用域错误:你在每个switch分支里重新定义dblTotalSales,导致每个分支的变量都是局部的,互相独立且离开分支就失效,自然无法在退出分支访问和累加。
  2. 累加逻辑错误:double dblTotalSales = +total;是重新赋值,不是累加,正确的累加写法是dblTotalSales += total;。
  3. 退出流程不合理:输入-1时仍会执行输入数量的步骤,属于无效操作。

修改后的代码

using System;
using static System.Console;

namespace Assignment_2
{
    class Program
    {
        static void Main(string[] args)
        {
            Write("Enter a salesperson's name: ");
            string name = ReadLine();

            // 把总额变量定义在循环外,确保所有分支都能访问
            double dblTotalSales = 0;

            do
            {
                Write("\nEnter an item number between 1 and 4 or -1 to quit: ");
                String item = ReadLine();
                int intItem = Convert.ToInt32(item);

                // 输入-1时直接输出总额并退出循环,跳过无效的数量输入
                if (intItem == -1)
                {
                    WriteLine($"Salesperson {name} sold a total of ${dblTotalSales:F2}");
                    break;
                }

                Write("\nEnter the quantity sold: ");
                String quantity = ReadLine();
                int intQuantity = Convert.ToInt32(quantity);

                switch (intItem)
                {
                    case 1:
                        {
                            double price = 239.99;
                            double total = price * intQuantity;
                            WriteLine($"Salesperson {name} sold {intQuantity} of item #{intItem} at ${total:F2}");
                            dblTotalSales += total; // 累加销售额
                            break;
                        }
                    case 2:
                        {
                            double price = 129.75;
                            double total = price * intQuantity;
                            WriteLine($"Salesperson {name} sold {intQuantity} of item #{intItem} at ${total:F2}");
                            dblTotalSales += total;
                            break;
                        }
                    case 3:
                        {
                            double price = 99.95;
                            double total = price * intQuantity;
                            WriteLine($"Salesperson {name} sold {intQuantity} of item #{intItem} at ${total:F2}");
                            dblTotalSales += total;
                            break;
                        }
                    case 4:
                        {
                            double price = 350.89;
                            double total = price * intQuantity;
                            WriteLine($"Salesperson {name} sold {intQuantity} of item #{intItem} at ${total:F2}");
                            dblTotalSales += total;
                            break;
                        }
                    default:
                        WriteLine("Invalid item number, please enter 1-4 or -1.");
                        break;
                }

            } while (true);
        }
    }
}

关键修改点

  • 将dblTotalSales定义在do-while循环外部,初始化为0,确保作用域覆盖整个循环和switch分支。
  • 增加intItem == -1的前置判断,直接输出总额并退出循环,避免无效操作。
  • 把赋值语句改为累加语句dblTotalSales += total;,实现销售额累计。
  • 用字符串插值$""替代字符串拼接,简化代码。
  • 增加default分支处理无效输入,提升程序健壮性。
  • 用:F2格式化金额输出,保留两位小数,符合货币显示规范。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 16:51:35