0

是否可以比较两个循环的总值?

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

我可以得到总数,但我不能比较它们,它只会比较循环的第一个值

4

2 回答 2

0

我建议重构代码,以便您的函数返回结果总数,然后直接比较主函数中的结果。像这样的东西应该工作:

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;
}
于 2013-06-28T16:48:15.223 回答
0

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 p.s.w.g 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);
    }
于 2013-06-28T17:19:41.270 回答