0

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

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

而(计数<= 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++;


}
4

1 回答 1

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 );
于 2014-12-15T14:25:06.787 回答