编写一个 Java 程序,通过示例查找字符串字符的 ASCII 值。在此示例中,我们使用了 String codePointAt() 函数来返回字符的 ASCII 码。
import java.util.Scanner;
public class ASCIIValuesOfStringChars {
private static Scanner sc;
public static void main(String[] args) {
String asciistr;
int i = 0;
sc= new Scanner(System.in);
System.out.print("\n Please Enter any Sentence for ASCII Codes : ");
asciistr = sc.nextLine();
while(i < asciistr.length())
{
System.out.println("The ASCII Value of " + asciistr.charAt(i) +
" Character = " + asciistr.codePointAt(i));
i++;
}
}
}
Java 字符串字符的 ASCII 值输出
Please Enter any Sentence for ASCII Codes : hello
The ASCII Value of h Character = 104
The ASCII Value of e Character = 101
The ASCII Value of l Character = 108
The ASCII Value of l Character = 108
The ASCII Value of o Character = 111
Java 程序使用 For 循环查找字符串字符的 ASCII 值
import java.util.Scanner;
public class ASCIIValuesOfStringChars1 {
private static Scanner sc;
public static void main(String[] args) {
String asciistr;
int i;
sc= new Scanner(System.in);
System.out.print("\n Please Enter any Sentence for ASCII Codes : ");
asciistr = sc.nextLine();
for(i = 0; i < asciistr.length(); i++)
{
System.out.println("The ASCII Value of " + asciistr.charAt(i) +
" Character = " + asciistr.codePointAt(i));
}
}
}

这是另一个示例,用于查找字符串中字符的 ASCII 值。在这里,我们使用 for 循环进行迭代,然后将字符类型转换为整数,这将显示该字符的 ASCII 值。
import java.util.Scanner;
public class ASCIIValuesOfStringChars2 {
private static Scanner sc;
public static void main(String[] args) {
String asciistr;
int i;
sc= new Scanner(System.in);
System.out.print("\n Please Enter any Sentence for ASCII Codes : ");
asciistr = sc.nextLine();
for(i = 0; i < asciistr.length(); i++)
{
char ch = asciistr.charAt(i);
int num = (int) ch;
System.out.println("The ASCII Value of " + ch +
" Character = " + num);
}
}
}
Please Enter any Sentence for ASCII Codes : Java Coding
The ASCII Value of J Character = 74
The ASCII Value of a Character = 97
The ASCII Value of v Character = 118
The ASCII Value of a Character = 97
The ASCII Value of Character = 32
The ASCII Value of C Character = 67
The ASCII Value of o Character = 111
The ASCII Value of d Character = 100
The ASCII Value of i Character = 105
The ASCII Value of n Character = 110
The ASCII Value of g Character = 103