【发布时间】:2017-11-14 19:14:38
【问题描述】:
这里是完整的源代码它没有输出正确的人,使最多和最少。我最好的猜测是循环内的 if 语句,如果该人工作超过 40 小时,则计算工资。代码很简单,学习 C#。解决此问题的最佳方法是什么....抱歉,我知道这可能是我缺少的一个简单问题。对标签的评论
{
class Program
{
const double FEDERAL_TAX_DEDUCTION = .10; //Federal Tax 10% of Gross Income
const double STATE_TAX_DEDUCTION = .05; //State Tax 5% of Gross Income
const int HOURS_OVERTIME = 40; //# of Hours Needed For Overtime Pay Rate
const double OVERTIME_PAY_DIFF = 1.5; //150% of Normal Pay
static void Main(string[] args)
{
int MAX_LIST_VALUE; //How Many Workers There Are For Array Limit.
Write("How Many Workers Are Currently Working? "); //Asks For User Input # Of Workers
MAX_LIST_VALUE = Convert.ToInt32(ReadLine());
string[] workerName = new string[MAX_LIST_VALUE];
for (int i = 0; i < MAX_LIST_VALUE; i++)
{
ForegroundColor = ConsoleColor.Blue;
WriteLine("Please Enter The Worker's Name: "); //Asks For User Input For Worker's Names
workerName[i] = ReadLine();
}
double[] workerWages = new double[MAX_LIST_VALUE];
for (int i = 0; i < MAX_LIST_VALUE; i++)
{
ForegroundColor = ConsoleColor.DarkYellow;
WriteLine("Please Enter " + workerName[i] + "'s Hourly Wage: "); //Asks For User Input For Worker's Wage
workerWages[i] = Convert.ToDouble(ReadLine());
}
double[] workerWeeklyHours = new double[MAX_LIST_VALUE];
for (int i = 0; i < MAX_LIST_VALUE; i++)
{
ForegroundColor = ConsoleColor.DarkCyan;
Write("How Many Hours Has {0} Worked This Week? ", workerName[i]); //Asks For User Input For Hours Worked
workerWeeklyHours[i] = Convert.ToDouble(ReadLine());
}
double[] workersRegularPay = new double[MAX_LIST_VALUE];
double[] workerGrossIncome = new double[MAX_LIST_VALUE];
double[] workerStateTaxAmount = new double[MAX_LIST_VALUE]; //All Calculation Arrays
double[] workerFederalTaxAmount = new double[MAX_LIST_VALUE];
double[] workerNetIncome = new double[MAX_LIST_VALUE];
double[] workerOvertimeHours = new double[MAX_LIST_VALUE];
double[] workerOvertimePay = new double[MAX_LIST_VALUE];
for (int i = 0; i < MAX_LIST_VALUE; i++)
{
if (workerWeeklyHours[i] > HOURS_OVERTIME) //If Hours # is > 40.
{
workerOvertimeHours[i] = workerWeeklyHours[i] - HOURS_OVERTIME;
workerOvertimePay[i] = workerOvertimeHours[i] * (workerWages[i] * OVERTIME_PAY_DIFF);
workerGrossIncome[i] = workerOvertimePay[i] + workersRegularPay[i];
}
else
{
workersRegularPay[i] = workerWeeklyHours[i] * workerWages[i];
workerGrossIncome[i] = workerWeeklyHours[i] * workerWages[i];
workerStateTaxAmount[i] = workerGrossIncome[i] * STATE_TAX_DEDUCTION;
workerFederalTaxAmount[i] = workerGrossIncome[i] * FEDERAL_TAX_DEDUCTION;
workerNetIncome[i] = workerGrossIncome[i] - workerFederalTaxAmount[i] - workerStateTaxAmount[i];
workerOvertimePay[i] = 0;
workerOvertimeHours[i] = 0;
}
}
ForegroundColor = ConsoleColor.Blue;
WriteLine("\nNAMES ARE BLUE.");
ForegroundColor = ConsoleColor.DarkYellow;
WriteLine("HOURLY WAGES ARE DARK YELLOW.");
ForegroundColor = ConsoleColor.DarkCyan;
WriteLine("HOURS WORKED ARE DARK CYAN.");
ForegroundColor = ConsoleColor.Green;
WriteLine("NET INCOMES ARE GREEN.");
ForegroundColor = ConsoleColor.Red;
WriteLine("-----------------------------");
WriteLine("There Are " + MAX_LIST_VALUE + " Workers!"); //Echo Of All Data Entered & Calculated For Workers
for (int i = 0; i < MAX_LIST_VALUE; i++)
{
ForegroundColor = ConsoleColor.Blue;
WriteLine("\nWorker's Name: " + workerName[i]);
WriteLine("-----------------------------");
ForegroundColor = ConsoleColor.DarkYellow;
WriteLine(workerName[i] + "'s Hourly Wage: " + workerWages[i].ToString("C"));
ForegroundColor = ConsoleColor.DarkCyan;
WriteLine(workerName[i] + "'s Hours Worked This Week: " + workerWeeklyHours[i]);
ForegroundColor = ConsoleColor.White;
WriteLine(workerName[i] + "'s Regular Pay: " + workersRegularPay[i].ToString("C"));
WriteLine(workerName[i] + "'s Gross Income Pay: " + workerGrossIncome[i].ToString("C"));
WriteLine(workerName[i] + "'s State Tax Amount: " + workerStateTaxAmount[i].ToString("C"));
WriteLine(workerName[i] + "'s Federal Tax Amount: " + workerFederalTaxAmount[i].ToString("C"));
ForegroundColor = ConsoleColor.Green;
WriteLine(workerName[i] + "'s Net Income: " + workerNetIncome[i].ToString("C"));
WriteLine(workerName[i] + "'s Overtime Hours: " + workerOvertimeHours[i]);
WriteLine(workerName[i] + "'s Overtime Pay (if any): " + workerOvertimePay[i].ToString("C"));
}
ForegroundColor = ConsoleColor.Red;
WriteLine("\nPress Enter To Continue For The Worker's That Earned The Least & The Most.");
ReadLine();
int minIndex = 0;
string workerLeast = "null";
double workerRegularPayLeast = 0,
workerGrossIncomeLeast = 0,
workerOverTimeLeast = 0;
int maxIndex = 0;
string workerMost = "null";
double workerRegularPayMost = 0,
workerGrossIncomeMost = 0,
workerOverTimeMost = 0;
for (int i = 0; i < workerGrossIncome.Length; i++) //Calculation For Worker That Made The LEAST
if (workerGrossIncome[i] < workerGrossIncome[minIndex])
{
minIndex = i;
workerLeast = workerName[i];
workerRegularPayLeast = workersRegularPay[i];
workerGrossIncomeLeast = workerGrossIncome[i];
workerOverTimeLeast = workerOvertimePay[i];
}
for (int i = 0; i < workerGrossIncome.Length; i++) //Calculation For Worker That Made The MOST
if (workerGrossIncome[i] >= workerGrossIncome[maxIndex])
{
maxIndex = i;
workerMost = workerName[i];
workerRegularPayMost = workersRegularPay[i];
workerGrossIncomeMost = workerGrossIncome[i];
workerOverTimeLeast = workerOvertimePay[i];
}
ForegroundColor = ConsoleColor.Green;
WriteLine("\nThe Worker That Earned The Least Is {0}.", workerLeast);
WriteLine("-----------------------------");
ForegroundColor = ConsoleColor.Magenta;
WriteLine("{0}'s Gross Income Was {1}.", workerLeast, workerGrossIncomeLeast.ToString("C"));
WriteLine("{0}'s Regular Pay Was {1}.", workerLeast, workerRegularPayLeast.ToString("C")); //Output For LEAST
WriteLine("{0}'s Overtime Pay Was {1}.", workerLeast, workerOverTimeLeast.ToString("C"));
ForegroundColor = ConsoleColor.Cyan;
WriteLine("\nThe Worker That Earned The Most Is {0}.", workerMost);
WriteLine("-----------------------------");
ForegroundColor = ConsoleColor.DarkGray;
WriteLine("{0}'s Gross Income Was {1}.", workerMost, workerGrossIncomeMost.ToString("C"));
WriteLine("{0}'s Regular Pay Was {1}.", workerMost, workerRegularPayMost.ToString("C")); //Output For MOST
WriteLine("{0}'s Overtime Pay Was {1}.", workerMost, workerOverTimeMost.ToString("C"));
ForegroundColor = ConsoleColor.Red;
WriteLine("\nPress Enter To Exit! \nThank You!");
ReadLine();
}
}
这就是我认为问题所在..
for (int i = 0; i < MAX_LIST_VALUE; i++)
{
if (workerWeeklyHours[i] > HOURS_OVERTIME) //If Hours # is > 40.
{
workerOvertimeHours[i] = workerWeeklyHours[i] - HOURS_OVERTIME;
workerOvertimePay[i] = workerOvertimeHours[i] * (workerWages[i] * OVERTIME_PAY_DIFF);
workerGrossIncome[i] = workerOvertimePay[i] + workersRegularPay[i];
}
else
{
workersRegularPay[i] = workerWeeklyHours[i] * workerWages[i];
workerGrossIncome[i] = workerWeeklyHours[i] * workerWages[i];
workerStateTaxAmount[i] = workerGrossIncome[i] * STATE_TAX_DEDUCTION;
workerFederalTaxAmount[i] = workerGrossIncome[i] * FEDERAL_TAX_DEDUCTION;
workerNetIncome[i] = workerGrossIncome[i] - workerFederalTaxAmount[i] - workerStateTaxAmount[i];
workerOvertimePay[i] = 0;
workerOvertimeHours[i] = 0;
}
逻辑问题 输出为:Output end only missing console inputs and first output
【问题讨论】:
-
您需要先创建一个Minimal, Complete, Verifiable example,然后任何人都可以帮助您
-
调试您的代码以确定问题的根源。请参阅:Navigating through Code with the Debugger。此外,使用包含 10 个属性的类类型的单个数组比使用 10 个数组要容易得多。
-
您的问题出在 if 块中,您在其中计算变量 workersRegularPay 和以下变量。当工人有加班时间时,您不计算正常时间。如果工人有加班时间,也应该计算 else 块。