编写一个 Java 程序,使用示例统计字符串中的字母、数字和特殊字符。在此 Java 统计数字、字母和字符串中特殊字符的示例中,我们首先使用 for 循环来迭代 aldisp_str。在循环中,为了保持代码简单,我们将每个字符分配给 ch (ch = aldisp_str.charAt(i))。接下来,我们使用了 Else If 语句。
在第一个 if 条件 (ch >= ‘a’ && ch <= ‘z’ || ch >= ‘A’ && ch <= ‘Z’) 中,我们检查字符是否是字母。如果为真,则增加 alph 的值。在 else if 语句中,我们使用 (ch >= ‘0’ && ch <= ‘9’) 来检查字符是否是数字,如果为真,则增加 digit 的值。否则,增加 special character 的值。
import java.util.Scanner;
public class CountAlpDigiSpl1 {
private static Scanner sc;
public static void main(String[] args) {
String aldisp_str;
int i, alph, digi, spl;
alph = digi = spl = 0;
char ch;
sc= new Scanner(System.in);
System.out.print("\nPlease Enter Alpha Numeric Special String = ");
aldisp_str = sc.nextLine();
for(i = 0; i < aldisp_str.length(); i++)
{
ch = aldisp_str.charAt(i);
if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' ) {
alph++;
}
else if(ch >= '0' && ch <= '9') {
digi++;
}
else {
spl++;
}
}
System.out.println("\nNumber of Alphabet Characters = " + alph);
System.out.println("Number of Digit Characters = " + digi);
System.out.println("Number of Special Characters = " + spl);
}
}

使用 While 循环的 Java 程序:统计字符串中的字母、数字和特殊字符
import java.util.Scanner;
public class CountAlpDigiSpl2 {
private static Scanner sc;
public static void main(String[] args) {
String aldisp_str;
int i, alph, digi, spl;
i = alph = digi = spl = 0;
char ch;
sc= new Scanner(System.in);
System.out.print("\nPlease Enter Alpha Numeric Special String = ");
aldisp_str = sc.nextLine();
while(i < aldisp_str.length())
{
ch = aldisp_str.charAt(i);
if(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' ) {
alph++;
}
else if(ch >= '0' && ch <= '9') {
digi++;
}
else {
spl++;
}
i++;
}
System.out.println("\nNumber of Alphabet Characters = " + alph);
System.out.println("Number of Digit Characters = " + digi);
System.out.println("Number of Special Characters = " + spl);
}
}
Please Enter Alpha Numeric Special String = 9In12&*belo 33j^%&*(?hi
Number of Alphabet Characters = 9
Number of Digit Characters = 5
Number of Special Characters = 9
在此 Java 统计数字、字母和字符串中特殊字符的示例中,我们比较的是 ASCII 值,而不是比较字符。
import java.util.Scanner;
public class CountAlpDigiSpl3 {
private static Scanner sc;
public static void main(String[] args) {
String aldisp_str;
int i, alph, digi, spl;
i = alph = digi = spl = 0;
int asci;
sc= new Scanner(System.in);
System.out.print("\nPlease Enter Alpha Numeric Special String = ");
aldisp_str = sc.nextLine();
for(i = 0; i < aldisp_str.length(); i++)
{
asci = aldisp_str.codePointAt(i);
if(asci >= 48 && asci <= 57 ) {
digi++;
}
else if(asci >= 65 && asci <= 90 || asci >= 97 && asci <= 122) {
alph++;
}
else {
spl++;
}
}
System.out.println("\nNumber of Alphabet Characters = " + alph);
System.out.println("Number of Digit Characters = " + digi);
System.out.println("Number of Special Characters = " + spl);
}
}
Please Enter Alpha Numeric Special String = Ja@#va2020!world2.0
Number of Alphabet Characters = 9
Number of Digit Characters = 6
Number of Special Characters = 4
在 Java 中,我们有一个内置函数 isdigit (Character.isDigit(ch)) 来检查字符是否是数字。isAlphabetic 函数 (Character.isAlphabetic(ch)) 用于检查字符是否是字母。如果这两个条件都为假,那么该字符就是特殊字符。
import java.util.Scanner;
public class CountAlpDigiSpl4 {
private static Scanner sc;
public static void main(String[] args) {
String aldisp_str;
int i, alph, digi, spl;
alph = digi = spl = 0;
char ch;
sc= new Scanner(System.in);
System.out.print("\nPlease Enter Alpha Numeric Special String = ");
aldisp_str = sc.nextLine();
for(i = 0; i < aldisp_str.length(); i++)
{
ch = aldisp_str.charAt(i);
if(Character.isDigit(ch)) {
digi++;
}
else if(Character.isAlphabetic(ch)) {
alph++;
}
else {
spl++;
}
}
System.out.println("\nNumber of Alphabet Characters = " + alph);
System.out.println("Number of Digit Characters = " + digi);
System.out.println("Number of Special Characters = " + spl);
}
}
Please Enter Alpha Numeric Special String = !@34*(Monday0?
Number of Alphabet Characters = 6
Number of Digit Characters = 3
Number of Special Characters = 5
这个 Java 代码 用于统计字符串中的字母、数字和特殊字符,与上面相同。这里,我们使用 Java 函数分离了字母、数字和特殊字符的逻辑。
import java.util.Scanner;
public class CountAlpDigiSpl5 {
private static Scanner sc;
public static void main(String[] args) {
String aldisp_str;
sc= new Scanner(System.in);
System.out.print("\nPlease Enter Alpha Numeric Special String = ");
aldisp_str = sc.nextLine();
CountDigitsAlphabetsSpecials(aldisp_str);
}
public static void CountDigitsAlphabetsSpecials(String aldisp_str) {
char ch;
int alph, digi, spl;
alph = digi = spl = 0;
for(int i = 0; i < aldisp_str.length(); i++)
{
ch = aldisp_str.charAt(i);
if(Character.isDigit(ch)) {
digi++;
}
else if(Character.isAlphabetic(ch)) {
alph++;
}
else {
spl++;
}
}
System.out.println("\nNumber of Alphabet Characters = " + alph);
System.out.println("Number of Digit Characters = " + digi);
System.out.println("Number of Special Characters = " + spl);
}
}
Please Enter Alpha Numeric Special String = Sep14<>?temb@r
Number of Alphabet Characters = 8
Number of Digit Characters = 2
Number of Special Characters = 4