简体   繁体   English

C#控制台应用程序-佣金计算器-如何将变量传递到Main()

[英]C# console application - commission calculator - how to pass variables into Main()

I can't figure out how to pass total, sale and comm into Main(). 我不知道如何将总计,销售和通讯传递到Main()中。

Anybody got an idea how to get those variables into Main and display (output) them there with the names? 有人知道如何将这些变量放入Main并在其中显示(输出)它们的名称吗?

Right now I can just output the variables in calcComm ... 现在,我可以在calcComm中输出变量。

Thanks in advance 提前致谢

Philip 菲利浦

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

namespace ConsoleApplication38
{
class Program
{

    public static void getsales()
    {
        string inputsales;
        double total = 0;
        double sale = 0;

        for (int salecount = 1; salecount <= 3; ++salecount)
        {

            Console.WriteLine("Enter sale: ");
            inputsales = Console.ReadLine();
            sale = Convert.ToDouble(inputsales);
            total = total + sale;

        }

        calcComm(total);

    }

    public static void calcComm(double total)
    {

        double comm = 0;
        comm = total * 0.2;
        Console.WriteLine(comm);

    }


    public static void Main () 
    {
        Console.WriteLine("           Sunshine Hot Tubs \n        Sales Commissions Report\n");
        char Letter;

        const string name1 = "Andreas";
        const string name2 = "Brittany";
        const string name3 = "Eric";
        string inputLetter;
        string name;
        Console.WriteLine("Please enter intial or type 'z' to quit");

        inputLetter = Console.ReadLine();
        Letter = Convert.ToChar(inputLetter);



        while (Letter != 'z')
        {

            if (Letter == 'a')
            {
                name = name1;
                getsales();
            }
            else if (Letter == 'b')
            {
                name = name2;
                getsales();
            }
            else if (Letter == 'e')
            {
                name = name3;
                getsales();
            }

                   else
                   {
                      Console.WriteLine("Invalid entry try again");
                   }



                   Console.WriteLine("Please enter intial or type z to quit");

                   inputLetter = Console.ReadLine();
                   Letter = Convert.ToChar(inputLetter);




        }
    }
 }
}

This gives an array of strings corresponding to the command line parameters. 这给出了与命令行参数相对应的字符串数组。

Main(string [] args)

By the way, when dealing with monetary units, it's better to use decimal than double. 顺便说一句,在处理货币单位时,最好使用十进制而不是双精度。

You should be using objects, then you can make those public. 您应该使用对象,然后可以将其公开。

class Sales
{
    public double total;
    public double sale;
    public double comm;
    ...

    public void CalcComm()
    {
       ...
    }
 }

Then you can reference them like this: 然后,您可以像这样引用它们:

 Sales.total, Sales.sale  

Or you can make them global but that's not normally advisable. 或者您可以将它们设置为全局,但是通常不建议这样做。

Look into the return keyword in C#; 查看C#中的return关键字; get your functions to return the relevant data back to main and have it make use of it. 让您的函数将相关数据返回给main并加以利用。

Consider this example for how to add command line arguments. 考虑此示例,了解如何添加命令行参数。 If you need them to be programmatically added consider writing a wrapper program and starting the Process inside it. 如果需要以编程方式添加它们,请考虑编写包装程序并在其中启动Process。

using System;

class Program
{
    static void Main(string[] args)
    {
    if (args == null)
    {
        Console.WriteLine("args is null"); // Check for null array
    }
    else
    {
        Console.Write("args length is ");
        Console.WriteLine(args.Length); // Write array length
        for (int i = 0; i < args.Length; i++) // Loop through array
        {
        string argument = args[i];
        Console.Write("args index ");
        Console.Write(i); // Write index
        Console.Write(" is [");
        Console.Write(argument); // Write string
        Console.WriteLine("]");
        }
    }
    Console.ReadLine();
    }
}

either you can build up a Data Transfer Object that holds all these three variables instantiate it and then return it to your main function. 或者,您可以建立一个包含这三个变量的数据传输对象实例化它,然后将其返回到您的主函数。

You could also make use of variables that are passed as references instead of by value and use the updated reference value. 您还可以使用作为引用而不是按值传递的变量,并使用更新的参考值。 Read about pass by value type & reference type for c# and the ref keyword. 了解有关c#的按值传递类型和引用类型以及ref关键字的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM