Take a char input from the Scanner

前端 未结 22 2255
[愿得一人]
[愿得一人] 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 06:07

    import java.util.Scanner;
    
    public class userInput{
        public static void main(String[] args){
            // Creating your scanner with name kb as for keyBoard
            Scanner kb = new Scanner(System.in);
    
            String name;
            int age;
            char bloodGroup;
            float height;
    
            // Accepting Inputs from user
            System.out.println("Enter Your Name");
            name = kb.nextLine(); // for entire line of String including spaces
            System.out.println("Enter Your Age");
            age = kb.nextInt(); // for taking Int
            System.out.println("Enter Your BloodGroup : A/B/O only");
            bloodGroup  = kb.next().charAt(0); // For character at position 0
            System.out.println("Enter Your Height in Meters");
            height = kb.nextFloat(); // for taking Float value
    
            // closing your scanner object
            kb.close();
    
            // Outputting All
            System.out.println("Name : " +name);
            System.out.println("Age : " +age);
            System.out.println("BloodGroup : " +bloodGroup);
            System.out.println("Height : " +height+" m");
    
        }
    }
    

提交回复
热议问题