简体   繁体   中英

Whats wrong with this my timer?

Ok so i am halfway done with this timer script but i noticed when it runs the functions the second time it does not take in inputs anymore any idea why? It does take in all and waits for inputs on start

public class ManagingLifts {


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        Timer timer = new Timer();

        DeliveryMan d = new DeliveryMan();
        Hacker h = new Hacker();
        Lift id = new Lift();
        ArrayList<Person> p = new ArrayList();
        ArrayList<String> who = new ArrayList();



        int MINUTES = 2; // setting the duration of timer for people entering lift



        timer.scheduleAtFixedRate(new TimerTask() // task to be run every 2 minute
        {
    @Override
    public void run()
    {
        int counter = 0;
        int HorD = (int)(Math.random() * 2) + 1; // decides who gets into the lift
        if (HorD <= 1)  // 
        {
            System.out.println("What is your name?");
            String name = sc.nextLine();

            System.out.println("What is your NRIC?");
            String nric = sc.nextLine();

            System.out.println("What is your Country?");
            String country = sc.nextLine();

            System.out.println("What floor do you wan to go?");
            int floor = sc.nextInt();

            if(floor >=50)  //making sure they only go to allowed floors
            {
                System.out.println("You can only access floors 40 - 50!");

            }

           Person list = new Person(name, nric);
           p.add(list);
           h.setCountry(country); 
           who.add("Hacker"); // to have a list of who entered the lift
           counter = +2;

        }

        else if (HorD >= 2)
        {

             System.out.println("What is your name?");
            String name2 = sc.nextLine();

            System.out.println("What is your NRIC?");
            String nric2 = sc.nextLine();

            System.out.println("What type of food are you delivering?");
            String food = sc.nextLine();

            System.out.println("What floor do you want to go?");
            int floor2 = sc.nextInt();

            if(floor2 >=31)
            {
                System.out.println("You can only access floors 2 - 30!");

            }

            Person list2 = new Person(name2, nric2);
            p.add(list2);
            d.setTypeofF(food);
            who.add("DeliveryGuy"); // who entered the lift

            counter = +2;
        }
           if(counter > 200) // stop running lift after 200min
        {
            timer.cancel();
        }


    }


        }, 0, 1000 * 60 * MINUTES); // will run this function every 2min




    }

}

When reading the floor number, you're reading only the next int, but not the enter to move to the next line. Since this is not read, the next time the loop starts, the name will be an empty string.

So add a sc.nextLine() when reading the floor, or just read a String and convert it to an int yourself using Integer.parseInt .

Alternatively, you can also discard all the input from System.in in between loops, by just recreating the Scanner . Instead of creating the Scanner outside the task, just create the Scanner as the first thing when your task runs (so inside the run method).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM