4

我的代码应该不断循环,直到输入“停止”作为员工姓名。我遇到的一个问题是,一旦它计算了第一个员工的工时和费率,它就会再次跳过员工姓名的提示。为什么?(代码在 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);
    }
}
4

3 回答 3

6

问题出在:

eName = input.nextLine();

将其更改为:

eName = input.next();

它会起作用的。

默认情况下java.util.Scanner,只要您按下回车键,类就会开始解析文本。因此,在循环中,您应该要求,input.next();因为已经在处理一行。

对于这两种方法,请参见 javadoc。

于 2011-02-22T04:43:03.253 回答
2

我假设它正在等待输入,input.nextLine()但提示没有出现。

我的猜测是某处存在缓冲问题 - 正在打印提示但输出没有到达屏幕。System.out.flush()在调用之前尝试调用input.nextLine()

于 2011-02-22T04:32:33.390 回答
0

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

Rate = input.nextDouble();

作为对这一行的响应,您可以键入“15.00”,然后按 {enter},这将在输入流中放置一个“新行”,从而形成输入流15.00\n。提取 Double 时,它​​会将“新行”留在输入流中。

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

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

于 2011-02-22T04:31:32.080 回答