工资单数组问题(不确定为什么不开始计算)

时间:2018-10-27 01:54:20

标签: c# arrays loops

public struct pay
    {
        public string name;
        public int rate;
        public int hours;
        public int gross;
        public int wtd;
        public int ssd;
        public int md;
        public int net;
    }
    static void Main(string[] args)
    {
        pay[] myPay = new pay[3];
        for (int i = 0; i<= 2; i++)
        {
            Console.WriteLine("Enter name: ");
            myPay[i].name = Console.ReadLine();
            Console.WriteLine("Enter pay rate: ");
            myPay[i].rate = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("Enter hours worked: ");
            myPay[i].hours = Convert.ToInt16(Console.ReadLine());
        }
        Console.WriteLine("Name\tRate\tHours\tGross\t W/T\tSS\tMed\tNet");
        for (int i = 0; i<=2; i++)
        {
            int gross = (myPay[i].rate * myPay[i].hours);
            int wtd = (myPay[i].gross * (1/20));
            int ssd = (myPay[i].gross * (3 / 100));
            int md = (myPay[i].gross * (1 / 100));
            int net = (myPay[i].gross - (md + ssd + wtd));
            Console.WriteLine(myPay[i].name + "\t" + myPay[i].rate + "\t" + myPay[i].hours + "\t" + myPay[i].gross + "\t" + myPay[i].wtd + "\t" + myPay[i].ssd + "\t" + myPay[i].md + "\t" + myPay[i].net);
        }
        Console.ReadLine();
    }
}

该代码旨在采用员工的姓名,费率和工作时间来查找总薪资。然后,将从总工资中提取成本(每个成本都用WTD,SD和MD表示),以找到该员工的净收入。

无论出于何种原因,总工资的计算都不会开始,因此以下计算也不会记录。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

如果需要十进制值,则不能将其存储在int

int (C# Reference)

如果您要计算货币,则需要使用decimal

decimal (C# Reference)

结构

public struct pay
{
   public string name; 
   public int rate;  
   public int hours;    
   public decimal gross;    
   public decimal wtd;    
   public decimal ssd;    
   public decimal md;    
   public decimal net;

   public void Calculate()
   {
      gross = (rate * hours);
      wtd = (gross * (1 / (decimal)20));
      ssd = (gross * (3 / (decimal)100));
      md = (gross * (1 / (decimal)100));
      net = (gross - (md + ssd + wtd));
   }
}

用法

static void Main(string[] args)
{
   pay[] myPay = new pay[3];
   for (int i = 0; i <= 2; i++)
   {
      Console.WriteLine("Enter name: ");
      myPay[i].name = Console.ReadLine();
      Console.WriteLine("Enter pay rate: ");
      myPay[i].rate = Convert.ToInt16(Console.ReadLine());
      Console.WriteLine("Enter hours worked: ");
      myPay[i].hours = Convert.ToInt16(Console.ReadLine());
   }

   Console.WriteLine("Name\tRate\tHours\tGross\t W/T\tSS\tMed\tNet");

   for (int i = 0; i <= 2; i++)
   {
      myPay[i].Calculate();
      Console.WriteLine($"{myPay[i] .name}\t{myPay[i] .rate}\t{myPay[i] .hours}\t{myPay[i] .gross}\t{myPay[i] .wtd}\t{myPay[i] .ssd}\t{myPay[i] .md}\t{myPay[i] .net}");
   }
   Console.ReadLine();
}

输出

Enter name:
dfg
Enter pay rate:
4
Enter hours worked:
5
Enter name:
dh
Enter pay rate:
56
Enter hours worked:
7
Enter name:
fjh
Enter pay rate:
56
Enter hours worked:
4
Name    Rate    Hours   Gross    W/T    SS      Med     Net
dfg     4       5       20      1.00    0.60    0.20    18.20
dh      56      7       392     19.60   11.76   3.92    356.72
fjh     56      4       224     11.20   6.72    2.24    203.84

免责声明 ,我只是解决了明显的问题,但是对于您为此编码所致或伤害的任何人,我概不负责。 < / p>

答案 1 :(得分:0)

我认为您的问题是将int用作变量,计算薪水或涉及货币的任何事物时,应使用十进制变量。