为什么我的程序在循环中跳过提示?

时间:2011-02-22 04:11:43

标签: java loops skip

我的代码应该不断循环,直到输入“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);
    }
}

3 个答案:

答案 0 :(得分:6)

问题在于:

eName = input.nextLine();

将其更改为:

eName = input.next();

它会起作用。

默认情况下,java.util.Scanner类会在您按输入键后立即开始解析文本。所以在循环内部,你应该要求input.next();,因为一条线已经被处理了。

请参阅两种方法的javadoc。

答案 1 :(得分:2)

我认为它正在等待input.nextLine()的输入但是没有出现提示。

我的猜测是某个地方存在缓冲问题 - 正在打印提示,但输出没有进入屏幕。在致电System.out.flush()之前尝试拨打input.nextLine()

答案 2 :(得分:0)

我的生活中从未使用过Java,但在这里......

Rate = input.nextDouble();

在回复此行时,您可以输入“15.00”,然后点击{enter},这会在输入流中添加“新行”,从而生成输入流15.00\n。提取Double时,它会使“新行”仍然在输入流中。

当您尝试提示用户输入其他员工的姓名时,它会读取输入流中仍有“新行”,并且会立即返回之前的内容,这是一个空字符串。

为了清除输入流,请运行一个虚拟.nextLine。