在第二次运行while循环时无法输入用户输入

时间:2014-04-03 01:45:42

标签: java while-loop user-input

我正在编写一个包含2个班级的小工资单程序(Payroll / contains main和Employee)。程序应继续询问用户输入,直到用户输入 stop 作为员工的姓名。在输入 stop 作为第一个员工的名字后,我能够让程序终止;但是,当我输入一个实际名称(例如Jim)时,程序会在while循环中运行一次。问题是当while循环再次运行时,它不允许任何用户输入员工姓名。它直接跳到下一行,这就要求工作时间。这是我在Employee类中的代码。

package payroll;

import java.util.Scanner;

public class Employee {

    // class variables
    private String employeeName;
    private double hoursWorked;
    private double payRate;
    private double weeklyPay;

    // Employee class constructor
    public Employee() {

    }

    // get value of class variable employeeName from user
    public void getInfo() {

        String name = ""; // initialized as empty string for use in while loop
        String term = "stop"; // terminating condition for while loop
        double hours; // employee hours worked
        double rate; // employee pay rate

        Scanner input = new Scanner(System.in);

        while (!name.equals(term)) { // will continue as long as name does not equal "stop"

            System.out.print("Enter the employee's name or stop if finished: ");
            name = input.nextLine(); // assigns the value input for name
            employeeName = name;

            if (employeeName.equals(term))
                break; // terminate while loop if user enters "stop"

            System.out.print("How many hours did " + employeeName + " work? ");
            hours = input.nextDouble(); // assigns the value input for hours
            hoursWorked = hours; // assigns hours to Employee class variable hoursWorked

            if (hours < 0) { // control statement to make sure hours is not a negative number
                System.out.print("Hours cannot be a negative number.\n");
                System.out.print("How many hours did " + employeeName + " work? ");
                hours = input.nextDouble(); // assigns the value input for hours
                hoursWorked = hours; // assigns hours to Employee class variable hoursWorked
            }

            System.out.print("Enter " + employeeName + "'s pay rate: ");
            rate = input.nextDouble(); // assigns the value input for rate
            payRate = rate; // assigns rate to Employee class variable payRate

            if (rate < 0) {
                System.out.print("Rate cannot be a negative number.\n");
                System.out.print("Enter " + employeeName + "'s pay rate: ");
                rate = input.nextDouble(); // assigns the value input for rate
                payRate = rate; // assigns rate to Employee class variable payRate
            }

            // calculates employee pay form calcPay method
            calcPay();

            // outputs message to console
            displayMessage();
        }
    }

public void calcPay() {

        weeklyPay = setHours() * setRate();
}

public void displayMessage() {

        System.out.printf("%s%s%.2f \n", employeeName, "'s weekly pay is $", weeklyPay);
}

这就是我的输出:

输入员工的姓名或完成后停止:Tim

蒂姆工作了几个小时? 40

输入Tim的工资率:20.50

蒂姆每周的工资为820美元

输入员工的姓名或完成后停止:工作了多少小时?

我无法理解为什么我不输入新员工的名字。

1 个答案:

答案 0 :(得分:0)

在使用Double支付费用后,您需要使用换行 ,然后才能阅读下一个Employee名称。

因此,只需在input.nextLine()循环结束前添加while即可。


示例代码:

while (!name.equals(term)) { // will continue as long as name does not equal "stop"
    System.out.print("Enter the employee's name or stop if finished: ");
    name = input.nextLine(); // assigns the value input for name

    // all your stuff... 

    System.out.print("Enter " + employeeName + "'s pay rate: ");
    rate = input.nextDouble(); // assigns the value input for rate

    // some more stuff... 

    if(input.hasNextLine()) {
        input.nextLine();
    }
}