我不能为这个薪资代码得到数学

时间:2015-03-04 18:31:14

标签: c#

我无法获得此代码的数学权利。我试图输出每天翻倍的薪水长达14天。例如:在第1天,输出应为1;在第2天,输出应为2;第3天输出应为4;在第14天,输出应为1050。

using System;

public class Salary
{
    public static void Main(string[] args)
    {
        int salary = 1;
        int w;

        Console.WriteLine("Enter number of days worked: ");
        w = Convert.ToInt32(Console.ReadLine());

        for (int wage = 1; wage < w + 1; wage++)
        {
            salary = wage * salary;
        }
        Console.WriteLine("The salary is: ${0}.00", salary);
        Console.WriteLine("Press any key to close....");
        Console.ReadKey();

    }
}

3 个答案:

答案 0 :(得分:0)

    for (int wage = 0; wage < w; wage++)
    {
        salary = salary * 2;
    }

答案 1 :(得分:0)

所以你真正做的是将薪水乘以2来增加一个权力(从0到13)。类似的东西:

public static void Main(string[] args)
{
    int salary = 1;
    int w;

    Console.WriteLine("Enter number of days worked: ");
    w = Convert.ToInt32(Console.ReadLine());

    salary *= Math.Pow(2, Math.Min(w - 1, 13));

    Console.WriteLine("The salary is: ${0}.00", salary);
    Console.WriteLine("Press any key to close....");
    Console.ReadKey();

}

答案 2 :(得分:0)

我认为薪水不会每天增加一倍,而是每天增加基本工资的100%。正如你所说,基本工资是1.第一天是1,第二天2,第三天3(如果是双倍,则为4)。如果我的理解是错误的,请更正。根据我的理解,我将代码更改为以下。

第14天的金额收入者的输出是105.00

public static void Main(string[] args)
        {
            int salary = 1;
            int w;
            int amountEarned = 0;

            Console.WriteLine("Enter number of days worked: ");
            w = Convert.ToInt32(Console.ReadLine());

            for (int wage = 1; wage < w + 1; wage++)
            {
                var currentDaySalary = (salary * ((wage*100)/100));
                amountEarned += currentDaySalary;
            }
            Console.WriteLine("The salary is: ${0}.00", amountEarned);
            Console.WriteLine("Press any key to close....");
            Console.ReadKey();

        }