简体   繁体   中英

Scanner problems in switch statement

I tried to add a switch statement in a method then put that method in another switch statement. It didn't work out as I expected it to... When I execute the program, the console wants me to immediately add a user input, which is not what I had in mind.

Here is what shows up in the console after I've executed the program:

qwerty

Greetings, dear Pilgrim. What is thine name?

Bob

Hello, Bob. Is thou ready to start thine quest? [Yes or No]

Please use capital letters... [Yes or No]

Application code

import java.util.Scanner;

public class rolePlay {

    static Scanner player = new Scanner(System.in);
    static String system;
    static String choice = player.nextLine();

    public void letterError() {
        System.out.println("Please use capital letters...");
        System.out.println(system);

        switch (choice) {

        case "Yes" : System.out.println("May thine travels be shadowed upon by Talos...");
        break;
        case "No" : System.out.println("We shall wait for thee...");
        break;
        default: 
        break;
        }
    }
    public rolePlay() {

    }

    public static void main(String[] args) {

        rolePlay you = new rolePlay();

        System.out.println("Greetings, dear Pilgrim. What is thine name?");
        String charName = player.nextLine();
        System.out.println("Hello, " + charName + ". Is thou ready to start thine quest?");

        system = "[Yes or No]";
        System.out.println(system);
        //String choice = player.nextLine();

        switch (choice) {

        case "Yes" : System.out.println("May thine travels be shadowed upon by Talos...");
        break;
        case "No" : System.out.println("We shall wait for thee...");
        break;
        default : you.letterError();
        break;
        }

        player.close();
    }
}
static String choice = player.nextLine();

This line will only be called once, when the class is first accessed. That's why it wants a user input immediately. You need to call player.nextLine() at the time you want to get user input; in this case, you should call it before each switch statement, like in the line you commented out.

Calling player.nextLine() and assigning it to your static variable choice causes the problem. Static variables are retrieved when the class is first called on, which in your case means before the main method is called. You should instead not assign a value to choice and assign player.nextLine() to choice inside of the main method when you're expecting the user to input something to console.

    System.out.println("Greetings, dear Pilgrim. What is thine name?");
    String charName = player.nextLine();
    System.out.println("Hello, " + charName + ". Is thou ready to start thine quest?");

    system = "[Yes or No]";
    System.out.println(system);
    choice = player.nextLine();     

should look like that after you remove player.nextLine() from the Static String choice declaration.

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