1

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




        }
    }
 }
}
4

5 に答える 5

3

これにより、コマンド ライン パラメータに対応する文字列の配列が得られます。

Main(string [] args)

ところで、通貨単位を扱うときは、2 倍よりも 10 進数を使用する方が適切です。

于 2012-05-08T02:37:51.033 に答える
2

オブジェクトを使用する必要があり、それらを公開できます。

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

    public void CalcComm()
    {
       ...
    }
 }

次に、次のように参照できます。

 Sales.total, Sales.sale  

または、それらをグローバルにすることもできますが、それは通常はお勧めできません。

于 2012-05-08T02:36:24.923 に答える
0

returnC#のキーワードを調べてください。関連するデータを返す関数を取得し、mainそれを利用させます。

于 2012-05-08T02:33:37.933 に答える
0

コマンドライン引数を追加する方法については、次の例を検討してください。それらをプログラムで追加する必要がある場合は、ラッパー プログラムを作成し、その中でプロセスを開始することを検討してください。

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();
    }
}
于 2012-05-08T02:36:39.840 に答える
0

これら 3 つの変数すべてを保持するデータ転送オブジェクトを構築し、それをインスタンス化してからメイン関数に返すことができます。

値ではなく参照として渡される変数を使用し、更新された参照値を使用することもできます。refC# とキーワードの値型と参照型による受け渡しについてお読みください。

于 2012-05-08T02:37:32.257 に答える