简体   繁体   English

C#如何比较2个循环值的结果

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

is it possible to compare the total value of both loops? 是否可以比较两个循环的总值?

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

i can get the totals but i just cant compare both of them , it will just compare the first value of the loop 我可以得到总数,但是我不能比较两者,它只会比较循环的第一个值

I'd recommend refactoring the code so that your functions return the resulting totals, and then directly comparing the results in your main function. 我建议重构代码,以便您的函数返回结果总数,然后直接在主函数中比较结果。 Something like this should work: 这样的事情应该起作用:

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

This issue is that you are passing the values for total "by value". 此问题是您要传递总计“按值”的值。 What that means is that the initial value of the int is passed into the function but any changes aren't applied to the original int (which is why when you do a comparison you get the initial value.) You can fix this in two ways. 这意味着将int的初始值传递到函数中,但不会将任何更改应用于原始int(这就是为什么在进行比较时会得到初始值的原因。)您可以通过两种方式解决此问题。 You can do what pswg did and return the values or you can tell your parameters to be passed "by reference". 您可以执行pswg的操作并返回值,也可以告诉您的参数“通过引用”传递。 In this case the values are linked to the initial variables passed in so changes can be seen outside of the function call. 在这种情况下,值链接到传入的初始变量,因此可以在函数调用之外看到更改。 To do that in C# you just add a "ref" keyword in front of the parameter in the method signature (where it is defined) and the method call (where you call it) like so: 要在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