Java中输出字符的ASCII值

1. 我们可以通过将字符强转为int型进行输出那么在控制台中我们将会得到字符的ascii值,这里我们使用nextLine()方法来接收字符串,可以接收空格/Tab键,使用next()方法则不会接收空格/Tab键,但是这里使用nextLine方法不能打印回车键的ascii值因为它遇到回车键就截止接收字符了

2. 具体的测试代码如下:

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        for(int i = 0; i < s.length(); i++){
            System.out.println((int)s.charAt(i));
        }
        sc.close();
    }
}

输入:0123456789

输出:

48
49
50
51
52
53
54
55
56
57
 

猜你喜欢

转载自blog.csdn.net/qq_39445165/article/details/84882569