C# 佣金计算器 - 如何将变量传递到 Main()

本文关键字:Main 变量 计算器 | 更新日期: 2023-09-27 18:33:38

我不知道如何将total,sale和comm传递到Main()。

有人知道如何将这些变量放入 Main 并在那里显示(输出)它们的名称吗?

现在我可以在 calcComm 中输出变量......

提前致谢

菲利普

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);


        }
    }
 }
}

C# 佣金计算器 - 如何将变量传递到 Main()

这给出了一个与命令行参数对应的字符串数组。

Main(string [] args)

顺便说一下,在处理货币单位时,最好使用十进制而不是双精度。

你应该使用对象,然后你可以公开这些对象。

class Sales
{
    public double total;
    public double sale;
    public double comm;
    ...
    public void CalcComm()
    {
       ...
    }
 }

然后你可以像这样引用它们:

 Sales.total, Sales.sale  

或者你可以把它们变成全球性的,但这通常是不可取的。

查看 C# 中的 return 关键字;让您的函数将相关数据返回到 main 并让它使用它。

请考虑此示例,了解如何添加命令行参数。如果需要以编程方式添加它们,请考虑编写包装程序并在其中启动 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();
    }
}
你可以

建立一个数据传输对象,其中包含所有这三个变量,实例化它,然后将其返回到你的主函数。

您还可以使用作为引用而不是按值传递的变量,并使用更新的引用值。阅读有关 C# 的按值类型和引用类型传递以及 ref 关键字的信息。