簡體   English   中英

C#如何比較2個循環值的結果

[英]C# How do i compare 2 result of looping values

是否可以比較兩個循環的總值?

class Program
{
    static void Main(string[] args)
    {
        int Op1_days = 0, Op1_salary = 0, Op2_days = 0, Op2_salary = 1, Op1_total = 0, Op2_total = 1;
        Console.WriteLine("Option 1");
        DisplayOption1(Op1_days, Op1_salary, Op1_total);

        Console.WriteLine("\nOption 2");
        DisplayOption2(Op2_days, Op2_salary, Op2_total);
        {
            Console.Write(Enter the option 1 total salary
    }
    static void DisplayOption1(int Op1_days, int Op1_salary, int Op1_total)
    {
        Console.WriteLine("Days   Salary");
        for (Op1_days = 1; Op1_days < 11; Op1_days++)
        {
            Op1_salary = Op1_salary + 100;
            Console.WriteLine("{0}      {1}", Op1_days, Op1_salary);
            Op1_total = (Op1_total + Op1_salary);
        }
        Console.WriteLine("Total of option 1 salary is = {0} ", Op1_total);
    }
    static void DisplayOption2(int Op2_days, int Op2_salary, int Op2_total)
    {
        Console.WriteLine("Days   Salary");
        Console.WriteLine("1      1");
        for (Op2_days = 2; Op2_days < 11; Op2_days++)
        {
            Op2_salary = Op2_salary * 2;
            Console.WriteLine("{0}      {1}", Op2_days, Op2_salary);
            Op2_total = (Op2_total + Op2_salary);
        }
        Console.WriteLine("Total of option 2 salary is = {0} ", Op2_total);
    }
}

我可以得到總數,但是我不能比較兩者,它只會比較循環的第一個值

我建議重構代碼,以便您的函數返回結果總數,然后直接在主函數中比較結果。 這樣的事情應該起作用:

static void Main(string[] args)
{
    int Op1_days = 0, Op1_salary = 0, Op2_days = 0, Op2_salary = 1;
    Console.WriteLine("Option 1");
    int Op1_total = DisplayOption1(Op1_days, Op1_salary, Op1_total);

    Console.WriteLine("\nOption 2");
    int Op2_total = DisplayOption2(Op2_days, Op2_salary, Op2_total);

    if (Op1_total == Op2_total)
    {
        Console.Write("The two salaries are equal");
    }
}

static int DisplayOption1(int Op1_days, int Op1_salary)
{
    int Op1_total = 0;
    Console.WriteLine("Days   Salary");
    for (Op1_days = 1; Op1_days < 11; Op1_days++)
    {
        Op1_salary = Op1_salary + 100;
        Console.WriteLine("{0}      {1}", Op1_days, Op1_salary);
        Op1_total = (Op1_total + Op1_salary);
    }
    Console.WriteLine("Total of option 1 salary is = {0} ", Op1_total);
    return Op1_total;
}
static void DisplayOption2(int Op2_days, int Op2_salary)
{
    int Op2_total = 0;
    Console.WriteLine("Days   Salary");
    Console.WriteLine("1      1");
    for (Op2_days = 2; Op2_days < 11; Op2_days++)
    {
        Op2_salary = Op2_salary * 2;
        Console.WriteLine("{0}      {1}", Op2_days, Op2_salary);
        Op2_total = (Op2_total + Op2_salary);
    }
    Console.WriteLine("Total of option 2 salary is = {0} ", Op2_total);
    return Op2_total;
}

此問題是您要傳遞總計“按值”的值。 這意味着將int的初始值傳遞到函數中,但不會將任何更改應用於原始int(這就是為什么在進行比較時會得到初始值的原因。)您可以通過兩種方式解決此問題。 您可以執行pswg的操作並返回值,也可以告訴您的參數“通過引用”傳遞。 在這種情況下,值鏈接到傳入的初始變量,因此可以在函數調用之外看到更改。 要在C#中做到這一點,您只需在方法簽名(定義位置)和方法調用(定義位置)的參數前面添加“ ref”關鍵字,如下所示:

static void Main(string[] args)
    {
        int Op1_days = 0, Op1_salary = 0, Op2_days = 0, Op2_salary = 1, Op1_total = 0, Op2_total = 1;
        Console.WriteLine("Option 1");
        DisplayOption1(Op1_days, Op1_salary, ref Op1_total);

        Console.WriteLine("\nOption 2");
        DisplayOption2(Op2_days, Op2_salary, ref Op2_total);

        Console.WriteLine("{0} Compared to {1}",Op1_total,Op2_total);
        Console.ReadLine();
    }
    static void DisplayOption1(int Op1_days, int Op1_salary, ref int Op1_total)
    {
        Console.WriteLine("Days   Salary");
        for (Op1_days = 1; Op1_days < 11; Op1_days++)
        {
            Op1_salary = Op1_salary + 100;
            Console.WriteLine("{0}      {1}", Op1_days, Op1_salary);
            Op1_total = (Op1_total + Op1_salary);
        }
        Console.WriteLine("Total of option 1 salary is = {0} ", Op1_total);
    }
    static void DisplayOption2(int Op2_days, int Op2_salary, ref int Op2_total)
    {
        Console.WriteLine("Days   Salary");
        Console.WriteLine("1      1");
        for (Op2_days = 2; Op2_days < 11; Op2_days++)
        {
            Op2_salary = Op2_salary * 2;
            Console.WriteLine("{0}      {1}", Op2_days, Op2_salary);
            Op2_total = (Op2_total + Op2_salary);
        }
        Console.WriteLine("Total of option 2 salary is = {0} ", Op2_total);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM