简体   繁体   English

虽然循环不重复

[英]While Loop not repeating

I have having a hard time getting this while loop to execute. 我很难让这个while循环执行。 I tried it without resetting the scanner first, and it would loop but throw the second line immediatly with the first line, so It was suggested to add the scanner reset lines. 我尝试了此操作,但没有先重置扫描仪,它会循环运行,但第二行与第一行立即抛出,因此建议添加扫描仪重置行。 Now the loop does not repeat at all....any suggestions? 现在循环根本不会重复。...有什么建议吗? I am looking at the first while loop in the main program, that is supposed to repeat the entire program until "quit" is entered into the empName field. 我正在看主程序中的第一个while循环,应该重复整个程序,直到在empName字段中输入“ quit”为止。 Not the smaller while loops nested in the middle. 不是较小的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();
   }
   }
}

Adding the third line of the following block (at the point where you read the employee name) is the way to fix this with the fewest changes to your code. 在下面的代码行中添加第三行(在您读取员工姓名的那一点上)是通过最少的代码更改即可解决此问题的方法。

 System.out.print( "Enter employee name or enter 'quit' when finished. " );
 empName = myScanner.nextLine();
 if(empName.equals("quit")) break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM