Take a char input from the Scanner

前端 未结 22 2833
[愿得一人]
[愿得一人] 2020-11-22 05:16

I am trying to find a way to take a char input from the keyboard.

I tried using:

Scanner reader = new Scanner(System.in);
char c = reade         


        
22条回答
  •  清歌不尽
    2020-11-22 05:58

    The easiest way is, first change the variable to a String and accept the input as a string. Then you can control based on the input variable with an if-else or switch statement as follows.

    Scanner reader = new Scanner(System.in);
    
    String c = reader.nextLine();
    switch (c) {
        case "a":
            
            break;
        case "b":
            
            break;
        default: 
            
    }
    

提交回复
热议问题