Java程序打印ASCII值
对于给定的字符,例如"ch",编写一个Java程序来打印其ASCII值。我们可以通过将任何字符赋给一个整数值并打印该整数值来找到该字符的ASCII值。
ASCII代表**美国信息交换标准代码**。有128个标准的ASCII代码,每个代码都可以用一个7位二进制数表示:0000000到1111111。扩展ASCII增加了额外的128个字符,这些字符在计算机、程序和字体之间有所不同。
示例场景
Input: character = s; Output: ascii_value = 115
示例1
在这个例子中,我们正在打印字符“s”的ASCII值。
public class AsciiValue{ public static void main(String[] args){ char my_input; my_input = 's'; System.out.println("The character has been defined as " +my_input); int ascii_value = my_input; System.out.println("The ASCII value of " + my_input + " is: " + ascii_value); } }
输出
The character has been defined as s The ASCII value of s is: 115
示例2
在这个Java程序中,我们正在打印英语字母A到Z的ASCII值。
public class AsciiValue { public static void main(String[] args) { for(char ch = 'A'; ch <= 'Z'; ch++) { int ascii_value = ch; System.out.print(ch + " = " + ascii_value + ", "); } } }
输出
A = 65, B = 66, C = 67, D = 68, E = 69, F = 70, G = 71, H = 72, I = 73, J = 74, K = 75, L = 76, M = 77, N = 78, O = 79, P = 80, Q = 81, R = 82, S = 83, T = 84, U = 85, V = 86, W = 87, X = 88, Y = 89, Z = 90,
示例3
类型转换是将给定数据类型转换为另一种数据类型的过程。在这里,我们使用类型转换来打印给定字符的ASCII值。
public class AsciiValue { public static void main(String[] args) { char my_input; my_input = 'd'; System.out.println("The ASCII value is: " + (int)my_input); } }
输出
The ASCII value is: 100
广告