Java 而循环不重复
我很难执行这个while循环。我在没有先重置扫描仪的情况下尝试了它,它会循环,但会立即抛出第二行和第一行,因此建议添加扫描仪重置行。现在循环根本不重复……有什么建议吗?我正在看主程序中的第一个while循环,它应该重复整个程序,直到在empName字段中输入“quit”。不是较小的while循环嵌套在中间。Java 而循环不重复,java,while-loop,Java,While Loop,我很难执行这个while循环。我在没有先重置扫描仪的情况下尝试了它,它会循环,但会立即抛出第二行和第一行,因此建议添加扫描仪重置行。现在循环根本不重复……有什么建议吗?我正在看主程序中的第一个while循环,它应该重复整个程序,直到在empName字段中输入“quit”。不是较小的while循环嵌套在中间。 Scanner input; empName = " "; while (!empName.equals("quit")) { input = new Scan
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();
}
}
}
扫描仪输入;
empName=“”;
而(!empName.equals(“退出”))
{
输入=新扫描仪(System.in);
系统输出打印(“输入员工姓名或完成后输入“退出”);
empName=myScanner.nextLine();
hourlyRate=-1;
while(hourlyRate添加下面代码块的第三行(在您读取员工姓名的位置)是修复此问题的方法,代码更改最少
System.out.print( "Enter employee name or enter 'quit' when finished. " );
empName = myScanner.nextLine();
if(empName.equals("quit")) break;
哪一个?请只发布相关代码。其中有3个,而s。我将进行一些测试输出,以找出循环条件失败的原因(或使用调试器)请将代码缩减到与之相关的部分。我怀疑这样做时,您会发现问题所在-如果没有,您应该以一个简短但完整的程序来演示问题…我希望它不会超过20行。myScanner
变量是什么?请提供我们不要缩减的源代码我认为您应该移动input=new扫描器(System.in);
退出而循环。