开发者

I am having trouble with the program skipping over the first input when looping. Please help

Ok, so I had to modify my already working program to use 2 separate classes...one to perform the task and one to store info. Once it does the first calculation and it gets to the second entry, it skips over employee name. Why? Please help. Here is the 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();
theEmploy开发者_开发问答ee.setHours(Hours);
while (Hours <0)                                                                 {                                                                                                                  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)                                                                 {                                                                                                                  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());
        }
    }
}


PART 2:

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


There are two places where an infinite loop can occurring, as Hours & Rate are not changing inside of them.

while (Hours <0)
{
  System.out.printf("Hours cannot be negative\n");
  System.out.printf("Please enter hours worked\n");
}

while (Rate <0)
{
  System.out.printf("Pay rate cannot be negative\n");
  System.out.printf("Please enter hourly rate\n");
}

Haven't read all the code, but it is likely this needs to be an if statement containing a continue.


You need hours= input.next double to change hour var so it fails the while condition

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜