C# 找到从阵列中获得最大收益的员工?
我已经设法得到最少的计算,但大多数代码是不工作的。。。我现在拥有的C# 找到从阵列中获得最大收益的员工?,c#,arrays,C#,Arrays,我已经设法得到最少的计算,但大多数代码是不工作的。。。我现在拥有的 for (int i = 0; i < workerGrossIncome.Length; i++) if (workerGrossIncome[i] > workerGrossIncome[maxIndex]) { maxIndex = i; workerMost = w
for (int i = 0; i < workerGrossIncome.Length; i++)
if (workerGrossIncome[i] > workerGrossIncome[maxIndex])
{
maxIndex = i;
workerMost = workerName[i];
workerRegularPayMost = workersRegularPay[i];
workerGrossIncomeMost = workerGrossIncome[i];
}
使用完整代码编辑:当前学习无法使用内置数组方法
但是,程序运行在计算工作周内赚得最多的工作人员时使用默认值
const double FEDERAL_TAX_DEDUCTION = .10; //10% of Gross Income
const double STATE_TAX_DEDUCTION = .05; //5% of Gross Income
const double workerOvertimePay = 0.00; //If Employee Does No Overtime
static void Main(string[] args)
{
int MAX_LIST_VALUE;
Write("How Many Worker's Are Working? ");
MAX_LIST_VALUE = Convert.ToInt32(ReadLine());
string[] workerName = new string[MAX_LIST_VALUE];
for (int i = 0; i < MAX_LIST_VALUE; i++)
{
WriteLine("Please Enter The Worker's Name: ");
workerName[i] = ReadLine();
}
double[] workerWages = new double[MAX_LIST_VALUE];
for (int i = 0; i < MAX_LIST_VALUE; i++)
{
WriteLine("Please Enter The Worker's Hourly Wage: ");
workerWages[i] = Convert.ToDouble(ReadLine());
}
double[] workerWeeklyHours = new double[MAX_LIST_VALUE];
for (int i = 0; i < MAX_LIST_VALUE; i++)
{
Write("How many hours has {0} worked this week? ", workerName[i]);
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];
double[] workerFederalTaxAmount = new double[MAX_LIST_VALUE];
double[] workerNetIncome = new double[MAX_LIST_VALUE];
for (int i = 0; i < MAX_LIST_VALUE; i++)
{
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];
}
WriteLine("There Are " + MAX_LIST_VALUE + " Workers!");
for (int i = 0; i < MAX_LIST_VALUE; i++)
{
WriteLine("Worker's Name: " + workerName[i]);
WriteLine(workerName[i] + " Hourly Wage: " + workerWages[i].ToString("C"));
WriteLine(workerName[i] + " Hours Wokred This Week: " + workerWeeklyHours[i]);
WriteLine(workerName[i] + " Regular Pay: " + workersRegularPay[i].ToString("C"));
WriteLine(workerName[i] + " Gross Income Pay: " + workerGrossIncome[i].ToString("C"));
WriteLine(workerName[i] + " State Tax Amount: " + workerStateTaxAmount[i].ToString("C"));
WriteLine(workerName[i] + " Federal Tax Amount: " + workerFederalTaxAmount[i].ToString("C"));
WriteLine(workerName[i] + " Net Income: " + workerNetIncome[i].ToString("C"));
}
ForegroundColor = ConsoleColor.Red;
WriteLine("\nPress Enter To Continue For Worker's That Earned The Least & 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++)
if (workerGrossIncome[i] < workerGrossIncome[minIndex])
{
minIndex = i;
workerLeast = workerName[i];
workerRegularPayLeast = workersRegularPay[i];
workerGrossIncomeLeast = workerGrossIncome[i];
}
for (int i = 0; i < workerGrossIncome.Length; i++)
if (workerGrossIncome[i] > workerGrossIncome[maxIndex]) //Doesnt calc the most..... FIX
{
maxIndex = i;
workerMost = workerName[i];
workerRegularPayMost = workersRegularPay[i];
workerGrossIncomeMost = workerGrossIncome[i];
}
ForegroundColor = ConsoleColor.Green;
WriteLine("\nThe Worker That Earned The Least Is {0}!", workerLeast);
WriteLine("{0}'s Gross Income Was {1}.", workerLeast, workerGrossIncomeLeast.ToString("C"));
WriteLine("{0}'s Regular Pay Was {1}.", workerLeast, workerRegularPayLeast.ToString("C"));
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("{0}'s Gross Income Was {1}.", workerMost, workerGrossIncomeMost.ToString("C"));
WriteLine("{0}'s Regular Pay Was {1}.", workerMost, workerRegularPayMost.ToString("C"));
WriteLine("{0}'s Overtime Pay Was {1}.", workerMost, workerOverTimeMost.ToString("C"));
ReadLine();
}
}
对于收入最高的工人来说,这是一个错误。剩下的就够了 没有林克
如果无法使用LINQ,请将maxIndex变量声明更改为:
int? maxIndex = null;
循环的开始是:
for (int i = 0; i < workerGrossIncome.Length; i++)
if (maxIndex == null || workerGrossIncome[i] > workerGrossIncome[maxIndex.Value])
选择将投影原始数据和索引。MaxBy将查找具有最大值的项。index将返回匹配项的索引。此处信息不足。我能看到的唯一一件事是第一次循环,它将索引0与自身进行比较……但我怀疑这是你的问题。如果最高工资的人在索引0,你就永远不会像你想的那样设置变量,因为WorkerStats来了[0]永远不会比它本身更伟大,因为它们是相等的。为什么不对列表进行排序,并选择薪酬最高或最低的?但是,我同意@BenHall的观点,我们需要更多的信息。我认为创建三个独立阵列的模型相当糟糕。通常在这种情况下,on会创建雇员的对象或结构。
// Sample Data
int[] workerGrossIncome = new[] {1, 2, 4};
var maxIndex = workerGrossIncome
.Select((value, index) => new {index, value})
.MaxBy(z => z.value)
.index;
var workerMost = workerName[i];
var workerRegularPayMost = workersRegularPay[i];
var workerGrossIncomeMost = workerGrossIncome[i];