Java中char的输入输出

Java中char的输入输出,第1张

Java中char的输入输出

我们可以发现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的博客-CSDN博客

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5707400.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存