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();

それが動作します。

デフォルトでは、 Enterキー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 に答える