简体   繁体   中英

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. You can do what pswg did and return the values or you can tell your parameters to be passed "by reference". 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM