public class WereWolfenstein2D {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        char c;
        String input;
        String difficulty;
        int difficulty1;
        String easy;

        System.out.println("Do you want to play? y/n");
        input = sc.nextLine( );
        input = input.toLowerCase();
        if (input.equals("y")) {
            System.out.println("Choose Difficulty ");
            System.out.println(" easy: type 1");
            System.out.println(" medium: type 2");
            System.out.println(" hard: type 3");
            difficulty = sc.nextLine( );
        }
    }
}


我有这个代码,我想拥有它,所以如果用户输入1则发生一堆东西,发生2件事等等。但是我不确定该如何处理,我不确定要如何放置if else语句或如何根据用户键入的内容告诉计算机转到该语句。

最佳答案

这是您应该如何进行

int difficulty = sc.nextInt();
switch (difficulty) {
    case 1:
        // code for easy
        break;
    case 2:
        // code for medium
        break;
    case 3:
        // code for hard
        break;
    default:
        throw new IllegalArgumentException("Unknown Difficulty");
}

07-24 19:35