我们可以发现Java中没有定义nextChar,因此不能简单的输入,下面列举两种
1.System.in.read()可以实现输入一个字符,返回字符的ASCII码,然后用强制类型转换转回字符
public class Dome {
public static void main(String[] args) throws Exception{
char c;
c = (char)System.in.read();
System.out.println(c);
}
}
2.通过录入字符串的第一个字符来实现
import java.util.Scanner;
public class Dome {
public static void main(String[] args) throws Exception{
char c;
Scanner sc = new Scanner(System.in);
c = sc.next().charAt(0);
System.out.println(c);
}
}
更多输入方法总结:Scanner输入的使用方法和一些注意(常见的输入,字符串的输入,char的输入)_Lkskywalker的博客-优快云博客