繁体   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