0

このwhileループを実行するのに苦労しています。最初にスキャナーをリセットせずに試してみましたが、ループしますが、最初の行ですぐに2行目がスローされるため、スキャナーのリセット行を追加することをお勧めします。今、ループはまったく繰り返されません....何か提案はありますか? メイン プログラムの最初の while ループを見ています。これは、「終了」が empName フィールドに入力されるまでプログラム全体を繰り返すことになっています。中央にネストされた小さい while ループではありません。

 Scanner input;
   empName = " ";
   while (!empName.equals("quit"))
   {
     input = new Scanner (System.in);
     System.out.print( "Enter employee name or enter 'quit' when finished. " );
     empName = myScanner.nextLine();
     hourlyRate = -1;
     while (hourlyRate <= 0)
     {
       System.out.print( "What is their hourly rate? $");
       hourlyRate = myScanner.nextDouble();
       if (hourlyRate <= 0)
       {
           System.out.println( "Value is not valid, please enter an amount above zero.");
       }
     }
     totHours = -1;
     while (totHours <= 0)
     {
         System.out.print( "How many hours did they work? ");
         totHours = myScanner.nextDouble();
         if (totHours <= 0)
         {
            System.out.println( "Value is not valid, please enter an amount above zero.");
         }
     }
     if (totHours > 40.00)//Calculate Pay and Taxes if OT
     {
        otHours = totHours - 40;
        regHours = totHours - otHours;
        otPay = (1.5 * hourlyRate) * otHours;
        regPay = hourlyRate * regHours;
        grossPay = regPay + otPay;
        taxes = grossPay * .13;
        netPay = grossPay - taxes;
//Display OT information
        System.out.print( "Employee name: ");
        System.out.println(empName);
        System.out.print( "Hourly Rate: ");
        System.out.println(money.format(hourlyRate));
        System.out.print( "Regular Hours Worked: ");
        System.out.println(regHours);
        System.out.print( "OT Hours Worked: ");
        System.out.println(otHours);
        System.out.print( "Total Hours Worked: ");
        System.out.println(totHours);
        System.out.println("   ");
        System.out.print( "Regular Pay = ");
        System.out.println(money.format(regPay));
        System.out.print( "Overtime Pay = ");
        System.out.println(money.format(otPay));
        System.out.print( "Gross Pay = ");
        System.out.println(money.format(grossPay));
        System.out.print( "Federal Taxes = ");
        System.out.println(money.format(taxes));
        System.out.println( "   ");
        System.out.print( "Net Pay = ");
        System.out.println(money.format(netPay));
     }
     else //Calculate No OT Pay and Taxes
     {
        grossPay = hourlyRate * totHours;
        taxes = .13 * grossPay;
        netPay = grossPay - taxes;
//Display No OT Information
        System.out.print( "Employee name: ");
        System.out.println(empName);
        System.out.print( "Hourly Rate: ");
        System.out.println(money.format(hourlyRate));
        System.out.print( "Hours Worked: ");
        System.out.println(totHours);
        System.out.println( "   ");
        System.out.print( "Gross Pay = ");
        System.out.println(money.format(grossPay));
        System.out.print( "Federal Taxes = ");
        System.out.println(money.format(taxes));
        System.out.println( "   ");
        System.out.print( "Net Pay = ");
        System.out.println(money.format(netPay));
        System.out.println( "   ");
     }
   String clearBuffer = input.nextLine();
   }
   }
}
4

1 に答える 1

1

次のブロックの 3 行目 (従業員名を読む場所) を追加することは、コードへの変更を最小限に抑えてこれを修正する方法です。

 System.out.print( "Enter employee name or enter 'quit' when finished. " );
 empName = myScanner.nextLine();
 if(empName.equals("quit")) break;
于 2013-01-27T20:44:11.713 に答える