具有while循环的Sentinel值

时间:2014-10-21 21:44:35

标签: java loops if-statement while-loop sentinel

我是Java的新手,正在上我的第一堂课。在过去的两周里,我一直在努力完成这项家庭作业,需要一些帮助。分配是编写一个工资单程序,提示工资率和工时。它需要连续循环,直到我输入一个标记值。一旦输入了哨兵价值,它就会为我支付毛费。

我终于把它带到了不断循环的地方,询问工资率和工时。它正确计算收入并将循环。

这是我的问题。我似乎无法获得-1的哨兵值。当我尝试设置它以便在我输入-1时它将结束,它不会做任何事情。在输入每组工资率和小时数之后,它还对收入进行平均。我希望它等到结束并平均所有收入之后。

请,任何输入都会有所帮助!

这是我到目前为止所拥有的。我从一个实现文件拉到一个测试文件。

import java.util.Scanner;

public class PayRoll 
{
    private double hours;
    private double payRate;
    private double grossPay;

    public void Payroll()
    {
        this.hours = hours;
        this.payRate = payRate;
    }

    public void setHours (double hours)
    {
        this.hours = hours;
    }

    public double getHours ()
    {
        return hours;
    }

    public void setPayRate (double payRate)
    {
        this.payRate = payRate;
    }

    public double getPayRate ()
    {
        return payRate;
    }

    public double getPay()
    {
        return (payRate * hours);
    }

    public void calcPay()
    {
        Scanner input = new Scanner(System.in);
        double total = 0;
        int payCounter = 0;

        System.out.printf("Enter employee payrate: $");
        setPayRate(input.nextDouble());

        System.out.printf("Enter employee hours worked: ");
        setHours(input.nextDouble());

        double payRate = getPayRate();

        while (payRate != -1) //Loops until the sentinel value is read. 
        {
            if (getHours() > 40)
            {
                System.out.printf("Total income is: $%.2f",
                        ((getPayRate() * 40)+((getHours() - 40)
                        * (getPayRate() * 1.5))));
                grossPay = ((getPayRate() * 40)+((getHours() - 40)
                        * (getPayRate() * 1.5)));

                payCounter++;
            }

            if (getHours() == 40)
            {
                System.out.printf("Total income is: $%.2f", ((getPayRate() * 40)));
                grossPay = ((getPayRate() * 40)); 

                payCounter++;
            }

            if (getHours() < 40)
            {
                System.out.printf("Total income is $%.2f%n",
                        ((getPayRate() * getHours())));
                grossPay = (getPayRate() * getHours());
                payCounter++;
            }

            if (payCounter!= 0)
            {
                total = total + grossPay; //Adds grade to total
                double average = (double) total / payCounter;

                System.out.printf(
                        "%n%nNumber of gross income entered: %d.%n%nTotal is $%.2f%n",
                        payCounter, total);
                System.out.printf("%nAverage income is: %.2f%n", average);
            }
            else 
            {
                System.out.println("No pay rate entered.");
            }

            System.out.printf("%nEnter employee payrate: $");
            setPayRate(input.nextDouble());

            System.out.printf("%nEnter employee hours worked: %n");
            setHours(input.nextDouble());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你在函数calcPay中只设置了一次double payRate,然后循环一个不会改变的条件(因为你不再设置它)。