java可以优先考虑执行哪个方法以及以哪种顺序执行?

时间:2011-02-26 09:24:44

标签: java optimization loops

我希望用户输入一两个数字,并强制他们在输入错误数据时再次执行此操作。我有一个想法,如果用户输入错误的号码,程序只会再次重复该问题。但是,我希望如果他们犯了错误,程序会停止所有其他活动,直到他们输入正确的数据,然后继续。

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

整个代码:

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

    public double getHourlyPayRate()
    {
        return hourpayrate;
    }

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

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

1 个答案:

答案 0 :(得分:0)

由于我不想编写完整的代码,我会对某些内容有所了解:

你可以将整个事情包装成:

while(true){  ...  }

然后在每次需要输入内容时暂停线程。在while循环结束时,检查他们是否输入了正确的答案。如果他们这样做,打破while循环。如果没有,请再次循环。

在while循环中执行以下操作:(伪代码)

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

它会再循环回来,给他们另一次机会。查看线程休眠/暂停以等待其输入。