为什么我的程序会循环跳过提示?
我的代码应该不断循环,直到输入“stop”作为员工姓名。我遇到的一个问题是,一旦计算出第一个员工的工作时间和费率,它将再次跳过输入员工姓名的提示。为什么? (顺便说一句,代码位于 2 个单独的类中)请帮忙。这是我的代码:
package payroll_program_3;
import java.util.Scanner;
public class payroll_program_3
{
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
employee_info theEmployee = new employee_info();
String eName = "";
double Hours = 0.0;
double Rate = 0.0;
while(true)
{
System.out.print("\nEnter Employee's Name: ");
eName = input.nextLine();
theEmployee.setName(eName);
if (eName.equalsIgnoreCase("stop"))
{
return;
}
System.out.print("\nEnter Employee's Hours Worked: ");
Hours = input.nextDouble();
theEmployee.setHours(Hours);
while (Hours <0) //By using this statement, the program will not
{ //allow negative numbers.
System.out.printf("Hours cannot be negative\n");
System.out.printf("Please enter hours worked\n");
Hours = input.nextDouble();
theEmployee.setHours(Hours);
}
System.out.print("\nEnter Employee's Rate of Pay: ");
Rate = input.nextDouble();
theEmployee.setRate(Rate);
while (Rate <0) //By using this statement, the program will not
{ //allow negative numbers.
System.out.printf("Pay rate cannot be negative\n");
System.out.printf("Please enter hourly rate\n");
Rate = input.nextDouble();
theEmployee.setRate(Rate);
}
System.out.print("\n Employee Name: " + theEmployee.getName());
System.out.print("\n Employee Hours Worked: " + theEmployee.getHours());
System.out.print("\n Employee Rate of Pay: " + theEmployee.getRate() + "\n\n");
System.out.printf("\n %s's Gross Pay: $%.2f\n\n\n", theEmployee.getName(),
theEmployee.calculatePay());
}
}
}
并且
package payroll_program_3;
public class employee_info
{
String employeeName;
double employeeRate;
double employeeHours;
public employee_info()
{
employeeName = "";
employeeRate = 0;
employeeHours = 0;
}
public void setName(String name)
{
employeeName = name;
}
public void setRate(double rate)
{
employeeRate = rate;
}
public void setHours(double hours)
{
employeeHours = hours;
}
public String getName()
{
return employeeName;
}
public double getRate()
{
return employeeRate;
}
public double getHours()
{
return employeeHours;
}
public double calculatePay()
{
return (employeeRate * employeeHours);
}
}
My code is supposed to continuously loop until "stop" is entered as employee name. A problem I am having is that once it does the calculation for the first employee's hours and rate, it will skip over the prompt for employee name again. Why? (The code is in 2 separate classes, btw) Please help. Here is my code:
package payroll_program_3;
import java.util.Scanner;
public class payroll_program_3
{
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
employee_info theEmployee = new employee_info();
String eName = "";
double Hours = 0.0;
double Rate = 0.0;
while(true)
{
System.out.print("\nEnter Employee's Name: ");
eName = input.nextLine();
theEmployee.setName(eName);
if (eName.equalsIgnoreCase("stop"))
{
return;
}
System.out.print("\nEnter Employee's Hours Worked: ");
Hours = input.nextDouble();
theEmployee.setHours(Hours);
while (Hours <0) //By using this statement, the program will not
{ //allow negative numbers.
System.out.printf("Hours cannot be negative\n");
System.out.printf("Please enter hours worked\n");
Hours = input.nextDouble();
theEmployee.setHours(Hours);
}
System.out.print("\nEnter Employee's Rate of Pay: ");
Rate = input.nextDouble();
theEmployee.setRate(Rate);
while (Rate <0) //By using this statement, the program will not
{ //allow negative numbers.
System.out.printf("Pay rate cannot be negative\n");
System.out.printf("Please enter hourly rate\n");
Rate = input.nextDouble();
theEmployee.setRate(Rate);
}
System.out.print("\n Employee Name: " + theEmployee.getName());
System.out.print("\n Employee Hours Worked: " + theEmployee.getHours());
System.out.print("\n Employee Rate of Pay: " + theEmployee.getRate() + "\n\n");
System.out.printf("\n %s's Gross Pay: $%.2f\n\n\n", theEmployee.getName(),
theEmployee.calculatePay());
}
}
}
AND
package payroll_program_3;
public class employee_info
{
String employeeName;
double employeeRate;
double employeeHours;
public employee_info()
{
employeeName = "";
employeeRate = 0;
employeeHours = 0;
}
public void setName(String name)
{
employeeName = name;
}
public void setRate(double rate)
{
employeeRate = rate;
}
public void setHours(double hours)
{
employeeHours = hours;
}
public String getName()
{
return employeeName;
}
public double getRate()
{
return employeeRate;
}
public double getHours()
{
return employeeHours;
}
public double calculatePay()
{
return (employeeRate * employeeHours);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于:
将其更改为:
它将起作用。
默认情况下,一旦您按下 enter 键,
java.util.Scanner
类就会开始解析文本。因此,在循环内,您应该请求input.next();
因为一行已经在处理中。请参阅 javadoc 了解这两种方法。
The problem is at:
Change this to:
It will work.
By default
java.util.Scanner
class starts parsing the text as soon as you hit the enter key. So inside the loop you should be asking forinput.next();
as a line is already being processed.See javadoc for both the methods.
我假设它正在等待
input.nextLine()
输入,但提示没有出现。我的猜测是某处存在缓冲问题 - 正在打印提示但输出没有到达屏幕。尝试在调用
input.nextLine()
之前调用System.out.flush()
。I assume that it is waiting for input at
input.nextLine()
but the prompt does not appear.My guess is that there is a buffering issue somewhere — that the prompt is being printed but the output is not getting to the screen. Try calling
System.out.flush()
before callinginput.nextLine()
.我一生中从未使用过 Java,但现在是这样...
Rate = input.nextDouble();
为了响应这一行,您可以输入“15.00”,然后按 {enter},这将在输入流中放置一个“新行”,使输入流
15.00\n
。提取 Double 时,它会将“新行”保留在输入流中。当您尝试提示用户输入另一个员工的姓名时,它会读取输入流中仍然有一个“新行”,并且它将立即返回之前的内容,即一个空字符串。
为了清除输入流,请运行虚拟 .nextLine。
Never used Java in my life, but here it goes...
Rate = input.nextDouble();
In response to this line, you might type "15.00" and then hit {enter}, which will put a "new line" in the input stream, making the input stream
15.00\n
. When extracting the Double, it will leave the "new line" still in the input stream.When you attempt to prompt the user for another employee's name, it reads that there is still a "new line" in the input stream, and it will immediately return what was prior to that, which is an empty string.
In order to clear out the input stream, run a dummy .nextLine.