从while循环中找到最高收入公司

时间:2014-12-15 14:17:14

标签: loops while-loop max highest

我需要找到收入最高的公司。

到目前为止,我能够从循环中获得最高的总收入,但不知道如何获得与此最高收益相关的公司名称。

while(count< = noOfComp)

{   
    System.out.print("Enter Company: ");
    companyName = kb.nextLine();

    System.out.print("Number of hires: ");
    noOfHires = kb.nextInt();
    kb.nextLine();


    //calculations
    totalEarnings = noOfHires * 2500 + 10000;
    System.out.println("Total Earnings of company is :" + totalEarnings);
    totalEarned = "" + totalEarnings;



    if(totalEarnings > large )                          //If statement for largest number
    {
     large = totalEarnings;
    }


    allTotalEarnings += totalEarnings;
    count++;


}

1 个答案:

答案 0 :(得分:0)

您可以在计算后通过与之前的最高计算结果进行比较,将最高收入公司名称及其收入分配给变量。

String highCompanyName = "";int highCompanyEarning = 0;
while( count <= noOfComp)
{   
    System.out.print("Enter Company: ");
    companyName = kb.nextLine();

    System.out.print("Number of hires: ");
    noOfHires = kb.nextInt();
    kb.nextLine();


    //calculations
    totalEarnings = noOfHires * 2500 + 10000;
    System.out.println("Total Earnings of company is :" + totalEarnings);
    totalEarned = "" + totalEarnings;


    if(totalEarnings> highCompanyEarning )  //If statement for largest number
    {
        highCompanyEarning = totalEarnings;
        highCompanyName = companyName;
    }


    allTotalEarnings += totalEarnings;//the purpose of it is not clear so left as it is.
    count++;
 }
 System.out.println("Highest Earnings company is :" + highCompanyName );
 System.out.println("Earning of that company is :" + highCompanyEarning );