1

我几乎让我的小控制台应用程序工作了,唯一的问题是变量totalComm的值一直为 0。

我尝试了一切,我可能只是忽略了一个可以解决问题的小修复。

提前致谢

菲利普

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

namespace CommissionCalculator
{
 class Program
  {
    public static void Main()
    {
        double total;
        double comm = 0;
        double totalComm = 0;
        string name = "";

        string inputLetter = "";
        int whilecount = 0;           

        while (inputLetter != "z")
        {
            if (whilecount == 0)
            {
                Title();
                whilecount = whilecount + 1;
            }

            getNameSales(out inputLetter, out name, out total, comm, totalComm);

            calcComm(total, out comm);

            outputVar(name, total, comm);

            totalCommCalc(comm, totalComm);
        }
    }

    public static void Title()
    {
        Console.WriteLine("\n           Sunshine Hot Tubs \n        Sales Commissions Report\n");
    }

    public static void getNameSales(out string inputLetter, out string name, out double total, double comm, double totalComm)
    {
        name = "";
        inputLetter = "";
        total = 0;

        Console.WriteLine("\nEnter salespersons initial a,b or e or enter z to quit");

        inputLetter = Console.ReadLine();

            if (inputLetter == "a")
            {
                name = "Andrea";

                string inValue;
                double sale = 0;
                total = 0;

                for (int count = 1; count <= 3; ++count)
                {
                    Console.WriteLine("Please enter sale: ");
                    inValue = Console.ReadLine();
                    sale = Convert.ToDouble(inValue);

                    total = total + sale;
                }
            }
            else if (inputLetter == "b")
            {
                name = "Brittany";

                string inValue;
                double sale = 0;
                total = 0;

                for (int count = 1; count <= 3; ++count)
                {
                    Console.WriteLine("Please enter sale: ");
                    inValue = Console.ReadLine();
                    sale = Convert.ToDouble(inValue);

                    total = total + sale;
                }
            }
            else if (inputLetter == "e")
            {
                name = "Eric";

                string inValue;
                double sale = 0;
                total = 0;

                for (int count = 1; count <= 3; ++count)
                {
                    Console.WriteLine("Please enter sale: ");
                    inValue = Console.ReadLine();
                    sale = Convert.ToDouble(inValue);

                    total = total + sale;
                }
            }
            else if (inputLetter == "z")
            {
                totalCommCalc(comm, totalComm);

                Console.WriteLine("Total commssion paid: {0:C}", totalComm);

                Console.ReadKey();
            }
        }

    public static void calcComm(double total, out double comm)
    {
        comm = total * 0.2;
    }

    public static double totalCommCalc(double comm, double totalComm)
    {            
        totalComm = totalComm + comm;

        return totalComm;
    }

    public static void outputVar(string name, double total, double comm)        
    { 
        Console.WriteLine("\n Name\t       Total sales\t     Commission \n{0}     \t    {1}     \t      {2}", name, total, comm);            
    }
  }
}
4

4 回答 4

1

totalComm 按传递给 totalCommCalc。要么通过引用传递它,要么从 totalCommCalc 返回它并分配回 totalComm。

当一个变量通过值传递时,一个副本被放置在您调用的方法的堆栈上。对该副本的更改不会影响原件。

当您通过引用传递时,原始地址被放置在堆栈上,并且更改确实会影响原始地址。

选项1:

// Passed by reference.
// Changes affect original.  
// Has side effects.  See http://en.wikipedia.org/wiki/Side_effect_%28computer_science%29
totalCommCalc(comm, ref totalComm);  

选项 2:

// Preferred.  Passed by value.  
// Result is assigned to a variable.  
// No side effects.
totalComm = totalCommCalc(comm, totalComm); 

我更喜欢第二种形式,因为随着代码的增长,具有副作用的代码可能更难理解和维护。

于 2012-05-09T01:32:52.350 回答
0

对“totalCommCalc”的调用应该存储返回值或使用 out 关键字,就像其他方法一样。为了与您的其他方法保持一致,我将使用 out 并删除返回值。

 totalCommCalc(comm, out totalComm);

或者

totalComm = totalCommCalc(comm, totalComm)
于 2012-05-09T01:32:43.293 回答
0

您是totalComm传递,而不是按引用传递。如果您希望更改totalComm保持不变,请将其设为ref参数,或者在适当的情况下从函数中返回值。

通过引用传递:

....
getNameSales(out inputLetter, out name, out total, comm, ref totalComm);
....

public static void getNameSales(out string inputLetter, out string name, out double total, double comm, ref double totalComm)
于 2012-05-09T01:34:01.153 回答
0

不要使用ref(根据@Eric J:随着代码的增长,具有副作用的代码可能更难理解和维护)

只需更改一行,即为您的变量分配函数返回值。

totalComm = totalCommCalc(comm, totalComm);

就是这样。

于 2012-05-09T01:34:12.367 回答