jeudi 23 mai 2019

Why is method keeping looping?

My method raceLevel starts from the beginning again and again even i insert one of the tree options and the second method speedAndKm wont be never executed. Even i delete first method from PSVM so i want to call speedAndKm method, the raceLevel method will be executed anyway. Thanks for your help!

public class test {

    public String raceLevel () {

        Scanner s = new Scanner (System.in);
        System.out.printf("Hi racer :)" + "%n");
        System.out.println("Select your game level: Enter a for basic; b for medium and c for professional");
        String choice = s.nextLine();

        String route;

        if (choice.equals("a")){
            System.out.println("Your race level: basic");
            System.out.println("----------------------------------------");
            System.out.println("You will get 3 points if you break a record 5:00min, otherwise 1 point");
            System.out.println("----------------------------------------");
            Route Barca = new Route(7);
            route = "Barca";
            System.out.println("Your route is:" + route);
        }
        else if (choice.equals("b")){
            System.out.println("Your race level: medium");
            System.out.println("----------------------------------------");
            System.out.println("You will get 4 points if you break a record 6:00min, otherwise 1 point");
            System.out.println("----------------------------------------");
            Route Paris = new Route(9);
            route = "Paris";
            System.out.println("Your route is:" + route);

        }
        else if (choice.equals("c")){
            System.out.println("Your race level: professional");
            System.out.println("----------------------------------------");
            System.out.println("You will get 5 points if you break a record 7:00min, otherwise 1 point");
            System.out.println("----------------------------------------");
            // Route Monaco = new Route(15);
            route = "Monaco";
            System.out.println("Your route is:" + route);
        }

        else {
            System.out.println("Wrong choice, try again!");
            choice = raceLevel();

        }
        return choice;
    }

    public static void main(String[] args) {
        test test = new test();
        test.raceLevel();
        test.speedAndKm();
    }


    public int speedAndKm() {
        Scanner p = new Scanner (System.in);
        Route r = new Route(0);

        int speed;

        System.out.println("----------------------------------------");
        System.out.println(r);
        System.out.println("insert speed you want to drive");
        speed = p.nextInt();
        System.out.println("your current speed is:" + speed);
        return speed;
           }
}

Aucun commentaire:

Enregistrer un commentaire