【问题标题】:Payroll Array Issues (unsure why calculations do not begin) [closed]工资单数组问题(不确定为什么不开始计算)[关闭]
【发布时间】:2018-10-27 01:54:19
【问题描述】:
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 表示)中扣除成本,以找到员工的净收入。

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

【问题讨论】:

  • 如果他们从未开始,会发生什么?旁注:1/20 将评估为 0,因为您正在使用整数并且 0.05 小于 1。因此,在所有情况下,wtd、ssd 和 md 将评估为 0。
  • 你能在控制台中显示你的程序的输出吗?
  • 您在运行此程序时在控制台中输入的 exact 值是什么?当您运行此代码时,究竟会发生什么?
  • 这将是学习使用调试器的绝佳时机。逐行浏览代码以查看发生了什么将帮助您自己快速解决此类问题。
  • string interpolation 是个好东西。

标签: c# arrays loops


【解决方案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

免责声明,我只是解决了明显的问题,但我不对您使用此代码造成的任何伤害或伤害负责

【讨论】:

    【解决方案2】:

    我认为你的问题是使用 int 作为变量,你应该在计算工资或任何涉及货币的东西时使用小数变量以获得准确的结果。

    【讨论】:

      猜你喜欢
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 2018-01-21
      • 2014-05-30
      • 2012-05-05
      • 2013-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多