Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
double d = sc.nextDouble();
String str = sc.next();
char c = sc.next().charAt(0);
String类中有一个方法为charAt()
public char charAt(int index)
返回指定索引处的 char 值。
- Scanner源码中,我们可知输入String类型实际是调用了next()方法,而输入字符实际上还是输入字符串,然后去字符串的第一个字符.
即str.charAt(0)
Scanner sc = new Scanner(System.in);
System.out.println("请输入字符");
char c = sc.next().charAt(0);
System.out.println("打印出来为:"+c);