Найти самую высокую доходную компанию из цикла

Мне нужно найти компанию с самым высоким заработком.

До сих пор я в состоянии извлечь самый высокий общий доход из цикла, но не знаю, как получить название компании, которое связано с этим самым высоким доходом.

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 ответ

Решение

Вы можете присвоить название компании с наибольшим доходом и ее доход в переменных после расчета, сравнив предыдущее самое высокое с рассчитанным.

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 );
Другие вопросы по тегам