C# 我有没有办法输出在循环中初始化的变量

C# 我有没有办法输出在循环中初始化的变量,c#,C#,在我的问题中,我试图为员工销售佣金输出销售摘要。所需的列之一是“销售”,它要求您获取该特定产品的销售数量。我遇到的问题是numberSold变量在While循环中被初始化,因此看起来我无法使用它。使用它的最佳选择是什么。任何关于如何使我的CommEarnings更干净的其他建议也将受到赞赏,因为我觉得有一种比我现在得到的更好的方法 decimal grossSales = 0; // total gross sales decimal earnings; // earnings made

在我的问题中,我试图为员工销售佣金输出销售摘要。所需的列之一是“销售”,它要求您获取该特定产品的销售数量。我遇到的问题是numberSold变量在While循环中被初始化,因此看起来我无法使用它。使用它的最佳选择是什么。任何关于如何使我的CommEarnings更干净的其他建议也将受到赞赏,因为我觉得有一种比我现在得到的更好的方法

decimal grossSales = 0; // total gross sales
    decimal earnings; // earnings made from sales
    int product = 0; // the product number
    int numberSold; // number sold of a given product
    decimal commission1 = .09M;
    decimal commission2 = .0925M;
    decimal commission3 = .0701M;
    decimal commission4 = .095M;
    decimal commission5 = .10M;
    decimal commission6 = .083M;
    decimal basePay = 250.00M;
    decimal product1CommEarnings = 0.00M;
    decimal product2CommEarnings = 0.00M;
    decimal product3CommEarnings = 0.00M;
    decimal product4CommEarnings = 0.00M;
    decimal product5CommEarnings = 0.00M;
    decimal product6CommEarnings = 0.00M;
    
    while ( product < 6 )
  {
     ++product;

     // prompt for and read number of the product sold from user
     Console.Write( "Enter number sold of product # {0} ->   ",
        product );
     numberSold = Convert.ToInt32( Console.ReadLine() );

        // determine gross of each individual product and add to total
        if (product == 1)
        {
            grossSales += numberSold * 239.99M;
            product1CommEarnings = Math.Round(commission1 * numberSold * 239.99M, 2);

        }
        else if (product == 2)
        {
            grossSales += numberSold * 129.75M;
            product2CommEarnings = Math.Round(commission2 * numberSold * 129.75M, 2);
        }

        else if (product == 3)
        {
            grossSales += numberSold * 99.95M;
            product3CommEarnings = Math.Round(commission3 * numberSold * 99.95M, 2);
        }

        else if (product == 4)
        {
            grossSales += numberSold * 350.89M;
            product4CommEarnings = Math.Round(commission4 * numberSold * 350.89M, 2);
        }
        else if (product == 5)
        {
            grossSales += numberSold * 100.00M;
            product5CommEarnings = Math.Round(commission5 * numberSold * 100.00M, 2);
        }
        else if (product == 6)
        {
            grossSales += numberSold * 1000.00M;
            product6CommEarnings = Math.Round(commission6 * numberSold * 1000.00M, 2);
        }
    } // end while loop

    Console.WriteLine("\n");
    Console.WriteLine("                   Commission                 Price                   Comm");
    Console.WriteLine("  Sales Summary      Earnings     Sold         Each    Extentsion     Rate");
    Console.WriteLine("  -------------    ----------     ----        -----     ---------     ----");
    Console.WriteLine($"  Base Pay            {basePay:C}                                        ");
    Console.WriteLine($"  Product 1             {product1CommEarnings}                                                      ");
    Console.WriteLine($"  Product 2             {product2CommEarnings}                                                      ");
    Console.WriteLine($"  Product 3             {product3CommEarnings}                                                      ");
    Console.WriteLine($"  Product 4            {product4CommEarnings}                                                       ");
    Console.WriteLine($"  Product 5             {product5CommEarnings}                                                      ");
    Console.WriteLine($"  Product 6            {product6CommEarnings}                                                       ");
decimal grossales=0;//总销售额
十进制收入;//销售收入
int product=0;//产品编号
int numberSold;//给定产品的销售数量
十进制佣金1=0.09M;
十进制佣金2=0.0925M;
十进制佣金3=0.0701M;
十进制佣金4=0.095M;
十进制佣金5=0.10M;
十进制佣金6=.083M;
十进制基本工资=250.00M;
十进制产品1共同收益=0.00M;
十进制产品2共同收益=0.00M;
十进制产品3共同收益=0.00M;
十进制产品4共同收益=0.00M;
十进制产品5共同收益=0.00M;
十进制收益=0.00M;
而(产品<6)
{
++产品;
//提示并读取从用户处销售的产品的编号
Console.Write(“输入产品的销售编号#{0}->”,
产品);
numberSold=Convert.ToInt32(Console.ReadLine());
//确定每项产品的毛额,并将其添加到总额中
如果(产品==1)
{
格罗萨莱斯+=号码牌*239.99米;
Product1共同收益=数学四舍五入(佣金1*numberSold*239.99M,2);
}
否则如果(产品==2)
{
Grossales+=桩号*129.75M;
Product2共同收益=数学圆(佣金2*numberSold*129.75M,2);
}
否则如果(产品==3)
{
格罗萨莱斯+=数倍*99.95米;
Product3Commerings=数学圆(佣金3*numberSold*99.95M,2);
}
否则如果(产品==4)
{
格罗萨莱斯+=号码牌*350.89米;
Product4共同收益=数学圆(佣金4*numberSold*350.89M,2);
}
否则如果(产品==5)
{
Grossales+=桩号*100.00M;
产品5共同收益=数学四舍五入(佣金5*numberSold*100.00M,2);
}
否则如果(产品==6)
{
格罗萨莱斯+=数倍*1000.00M;
Product6Comm盈余=数学整数(佣金6*numberSold*1000.00M,2);
}
}//在循环结束时结束
Console.WriteLine(“\n”);
Console.WriteLine(“佣金价格通信”);
Console.WriteLine(“销售汇总-每延期销售收益率”);
Console.WriteLine(“-----------------------------”;
Console.WriteLine($“基本工资{basePay:C}”);
Console.WriteLine($“产品1{product1Commercing}”);
Console.WriteLine($“产品2{product2commerains}”);
Console.WriteLine($“产品3{product3Commercing}”);
Console.WriteLine($“产品4{product4Commercing}”);
Console.WriteLine($“产品5{product5commincomen}”);
Console.WriteLine($“产品6{product6commearning}”);

您应该初始化数字密码

int numberSold = 0; //initial value will be discarded by the loop.
但是,当您在循环中重写每个项目时,仍然无法获得每个项目的编号,并且打印的值将是循环最后一次迭代的值


使用记录类或元组保存每次迭代的值。

您应该初始化numberSold

int numberSold = 0; //initial value will be discarded by the loop.
但是,当您在循环中重写每个项目时,仍然无法获得每个项目的编号,并且打印的值将是循环最后一次迭代的值


使用记录类或元组保存每次迭代的值。

Q:有没有办法输出在循环中初始化的变量?A:当然可以。只需在循环之外声明变量。问:您所展示的代码是否存在特定问题?
numberSold
与其他变量一起在循环外声明,因此,如果您遇到错误,您应该共享该错误,以便我们能够解决正确的问题。不确定我是如何错过在循环外声明的,看起来我需要做的只是将其分配给变量。很抱歉,谢谢你的帮助。也请考虑使用A,而不是一堆“I/OR”语句。而且一定要确保您始终能够检测和处理“以上任何一项”的情况!如果你当月最大的卖家是“产品7”?;)@paulsm4 if/elseQ有什么问题:有没有办法输出在循环中初始化的变量?A:当然可以。只需在循环之外声明变量。问:您所展示的代码是否存在特定问题?
numberSold
与其他变量一起在循环外声明,因此,如果您遇到错误,您应该共享该错误,以便我们能够解决正确的问题。不确定我是如何错过在循环外声明的,看起来我需要做的只是将其分配给变量。很抱歉,谢谢你的帮助。也请考虑使用A,而不是一堆“I/OR”状态。