开发者

Can java prioritize which method executes and in which order?

I want the user to enter a number or two, and force them to do it over again if they input wrong data. I had the idea that if the user enters a wrong number the program just repeats the question again. However, I want it such that if they make a mistake the program stops all other activity until they input the right data, and then continues.

public int getShift()
{
    Scanner keyboard= new Scanner(System.in);

    for(int shift = 1; shift == 1;)
    {
        System.out.println("It's the Day Shift");
        break;
    }

    for(int shift = 2; shift == 2;)
    {
        System.out.println("It's the Night Shift");
        break;
    }

    while (shift < 0 | shift > 3);
    {
        System.out.println("Sorry enter a number one or two:");
        System.out.println("");
        System.out.println("Enter the employee Shift: ");

        shift=keyboard.nextInt();;
    }

    return shift;
}

The whole code:

import java.util.Scanner;

class Employee
{
    //fields
    private String Empname;
    private String Empnumber;
    private String Hiredate;

    //parameterized constructor
    public Employee(String Empname,String Empnumber,
                    String Hiredate)
    {
        setName(Empname);
        setNumber(Empnumber);
        setHireDate(Hiredate);
    }

    //Mutator functions
    //sets employee name
    public void setName(String n)
    {
        Empname = n;
    }

    //sets employee number
    public void setNumber(String num)
    {
        Empnumber = num;
    }

    //sets hire date
    public void setHireDate(String h)
    {
        Hiredate = h;
    }

    /* Accessor functions */
    //returns employee name
    public String getName()
    {
        return Empname;
    }

    //returns employee number
    public String getNumber()
    {
        return Empnumber;
    }

    //returns hire date
    public String getHireDate()
    {
        return Hiredate;
    }
}

class ProductionWorker extends Employee
{
    //fields
    private int shift;
    private double hourpayrate;

    //constructor
    public ProductionWorker(String Empname,
                            String Empnumber,
                            String Hiredate,int shift,
                            double hourpayrate)
    {
        super(Empname,Empnumber,Hiredate);

        setShift(shift);
        setHourlyPayRate(hourpayrate);
    }

    //accessor
    public int getShift()
    {
        Scanner keyboard= new Scanner(System.in);

        for(int shift = 1; shift == 1;)
        {
            System.out.println("It's the Day S开发者_运维问答hift");
            break;
        }

        for(int shift = 2; shift == 2;)
        {
            System.out.println("It's the Night Shift");
            break;
        }

        while (shift < 0 | shift > 3);
        {
            System.out.println("Sorry enter a number one or two:");
            System.out.println("");
            System.out.println("Enter the employee Shift: ");

            shift=keyboard.nextInt();;
        }

        return shift;
       }

    public double getHourlyPayRate()
    {
        return hourpayrate;
    }

    //mutator
    public void setShift(int s)
    {
        shift = s;
    }

    public void setHourlyPayRate(double r)
    {
        hourpayrate = r;
    }
}


I'm going to be general about some stuff since I dont want to write your whole code:

You could wrap the entire thing in:

while(true){  ...  }

Then pause the thread at each time where they need to input something. At the end of this while loop, do a check for if they input a correct answer. If they did, break the while loop. If not, loop again.

Inside the while loop do this: (pseudo code)

if input == 1
    //theyre the day shift, run some dayshift code
    break;
elseif input == 2
    //theyre the night shift, run some nightshift code
    break;
else
    //they didn't enter a valid choice
    System.out.print("Please enter 1 or 2");

It will loop back up again, and give them another chance. Look into thread sleeping/pausing to wait for their input.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜