Mein Code soll eine Endlosschleife durchlaufen, bis "stop" als Mitarbeitername eingegeben wird. Ein Problem, das ich habe, ist, dass nach der Berechnung der Stunden und des Satzes des ersten Mitarbeiters die Eingabeaufforderung für den Mitarbeiternamen wieder übersprungen wird. Warum? (Der Code ist in 2 separaten Klassen, btw) Bitte helfen Sie. Hier ist mein 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());
}
}
}
UND
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);
}
}