找到从阵列中获得最大收益的员工?

时间:2017-11-10 21:12:23

标签: c# arrays

我设法得到了#34;至少"然而,计算了大多数"的代码。不工作......我现在有什么。

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();
    }
}

错误是工人收入最高的循环。其余的很好,因为需要什么。

1 个答案:

答案 0 :(得分:0)

没有LINQ

如果您不能使用LINQ,请将您的flag = 0; function openpara(val) { calc.display.value += val; flag += 1; } function closepara(valval) { calc.display.value += valval; flag -= 1; } function backspace(calc) { var size = calc.display.value.length; calc.display.value = calc.display.value.substring(0, size - 1); } function Resetfunction(calc) { calc.display.value = ""; flag = 0; } function cos_function() { flag += 1; calc.display.value += Math.cos(); } function sin_function() { flag += 1; calc.display.value += Math.sin(); } function tan_function() { flag += 1; calc.display.value += Math.tan(); } function log_function() { flag += 1; calc.display.value += Math.log(); } function sqrt_function() { flag += 1; calc.display.value += Math.sqrt(); } function exp_function() { flag += 1; calc.display.value += 'Math.exp('; } function fact(x) { factvar = 1; for (i = 1; i <= x; i++) { factvar = factvar * i; } return factvar; } function fact_function(x) { flag += 1; calc.display.value += 'fact('; } function power_function(x) { flag += 1; calc.display.value += Math.pow(x,y); } function evaluation(calc) { n = calc.display.value; var size = calc.display.value.length; var lastchar = calc.display.value.charAt(size) if (isNaN(lastchar) && lastchar != ")" && lastchar != "!") { calc.display.value = "syntax error"; } else if (flag != 0) { calc.display.value = "error:paranthesis"; } else { result = eval(n); calc.display.value = result; } } 变量声明更改为:

maxIndex

并开始您的循环:

int? maxIndex = null;

以确保代码在第一个元素是最大元素时仍然有效。

您的如果第一个元素是最大的,则现有代码不起作用,因为for (int i = 0; i < workerGrossIncome.Length; i++) if (maxIndex == null || workerGrossIncome[i] > workerGrossIncome[maxIndex.Value]) workerGrossIncome[i] > workerGrossIncome[maxIndex],因此false等不会被设置。

< h3>使用LINQ

如果添加MoreLINQ Nuget Package,则可以按如下方式计算workerMost

maxIndex

// 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]; 将投影原始数据索引。 Select会找到MaxBy最大的项目。 value将返回该匹配项的索引。