Java教程

如何在Java中打印ASCII值

ASCII (美国信息交换标准代码)的缩写。它是一个7位字符集,包含 128 (0到127)个字符。它代表一个字符的数值。例如, A ASCII值 65
在本节中,我们将学习 通过 Java 程序打印ASCII值代码
两种打印ASCII的方法Java中的值:
将变量分配给int变量 使用类型转换

将变量分配给int变量

要打印ASCII字符的值,我们不需要使用任何方法或类。 Java在内部将字符值转换为ASCII值。
让我们找到ASCII值通过Java程序的字符。
在以下程序中,我们分配了两个字符分别在 ch1 和 ch2 变量中> a 和 b 。为了找到 a b 的ASCII值,我们已将ch1和ch2变量分配给整数变量 asciivalue1 asciivalue2,。最后,我们打印了变量 asciivalue1 asciivalue2 ,其中存储了字符的ASCII值。
PrintAsciiValueExample1.java
public class PrintAsciiValueExample1 {
    public static void main(String[] args) {
        // character whose ASCII value to be found
        char ch1 = 'a';
        char ch2 = 'b';
        // variable that stores the integer value of the character
        int asciivalue1 = ch1;
        int asciivalue2 = ch2;
        System.out.println("The ASCII value of " + ch1 + " is: " + asciivalue1);
        System.out.println("The ASCII value of " + ch2 + " is: " + asciivalue2);
    }
}
输出:
The ASCII value of a is: 97
The ASCII value of b is: 98
编写上述程序的另一种方法是:
PrintAsciiValueExample2.java
public class PrintAsciiValueExample2 {
    public static void main(String[] String) {
        int ch1 = 'a';
        int ch2 = 'b';
        System.out.println("The ASCII value of a is: "+ch1);
        System.out.println("The ASCII value of b is: "+ch2);
    }
}
输出:
The ASCII value of a is: 97
The ASCII value of b is: 98
类似地,我们可以打印其他字符(A,B,C,....,Z)和符号(!,@,$,*等)的ASCII值。

使用类型强制转换

类型强制转换是一种将变量转换为另一种数据类型的方法。
在以下程序中,我们声明了两个变量 ch1 ch2 类型为 char 的字符,分别具有字符 a b 。在接下来的两行中,我们使用 (int)将char类型转换为int类型。执行这两行后,变量 ch1 ch2 分别转换为整数变量 ascii1 ascii2
最后,我们已经打印了变量 ascii1 ascii2 ,其中存储了字符的ASCII值。
PrintAsciiValueExample3.java
public class PrintAsciiValueExample3 {
    public static void main(String[] args) {
        //characters whose ASCII value to be found
        char ch1 = 'a';
        char ch2 = 'b';
        //casting or converting a charter into int type
        int ascii1 = (int) ch1;
        int ascii2 = (int) ch2;
        System.out.println("The ASCII value of " + ch1 + " is: " + ascii1);
        System.out.println("The ASCII value of " + ch1 + " is: " + ascii2);
    }
}
输出:
The ASCII value of a is: 97
The ASCII value of b is: 98
如果我们不想分配字符,我们也可以从用户那里获取字符。
PrintAsciiValueExample4.java
import java.util.Scanner;
public class PrintAsciiValueExample4 {
    public static void main(String args[]){
        System.out.print("Enter a character: ");
        Scanner sc = new Scanner(System.in);
        char chr = sc.next().charAt(0);
        int asciiValue = chr;
        System.out.println("ASCII value of " +chr+ " is: "+asciiValue);
    }
}
输出1:
Enter a character: P
ASCII value of P is: 80
输出2:
Enter a character: G
ASCII value of G is: 71
以下程序打印所有字符的ASCII值(0到255)。在输出中,我们显示了一些值。
AsciiValueOfAllChracters.java
public class AsciiValueOfAllChracters {
    public static void main(String[] args) {
        for(int i = 0;i <= 255;i++){
            System.out.println(" The ASCII value of " + (char)i + " = " + i);
        }
    }
}
输出:
如何在Java中打印ASCII值
如果要打印所有字母(A到Z)的ASCII值,我们可以在循环中设置值并打印它们。
AsciiValueAtoZ.java
public class AsciiValueAtoZ
{
    public static void main(String[] args)
{
        for(int i = 65;
        i <
        = 90;
        i++)
{
            System.out.println(" The ASCII value of " + (char)i + " = " + i);
        }
    }
}
输出:
ASCII值打印
如何类似地,我们可以通过更改上面代码中的循环,将 a的ASCII值打印到Java
for(int i = 97;i <= 122;i++)

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4