作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很难让这个 while 循环执行。我在没有先重置扫描仪的情况下尝试了它,它会循环但会立即将第二行与第一行一起抛出,因此建议添加扫描仪重置行。现在循环根本不重复......有什么建议吗?我正在查看主程序中的第一个 while 循环,它应该重复整个程序,直到在 empName 字段中输入“quit”。不是嵌套在中间的较小的 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();
}
}
}
最佳答案
添加以下 block 的第三行(在您读取员工姓名的位置)是通过对代码进行最少更改来解决此问题的方法。
System.out.print( "Enter employee name or enter 'quit' when finished. " );
empName = myScanner.nextLine();
if(empName.equals("quit")) break;
关于java - While 循环不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14551866/
我是一名优秀的程序员,十分优秀!