java编写程序:任意输入一行英文(包括空格),统计其中英文字母、数字和其他符号各有多少个,并输出结果。
时间: 2024-05-07 16:16:58 浏览: 16
好的,你的问题是如何使用Java编写程序,统计一行英文中英文字母、数字和其他符号的数量。下面是一个简单的Java程序,可以实现这个功能:
```
import java.util.Scanner;
public class CountCharacters {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a line of English text: ");
String text = input.nextLine();
int letters = 0;
int digits = 0;
int others = 0;
for(int i=0; i<text.length(); i++){
char ch = text.charAt(i);
if(Character.isLetter(ch)){
letters++;
}else if(Character.isDigit(ch)){
digits++;
}else{
others++;
}
}
System.out.println("Letters: " + letters);
System.out.println("Digits: " + digits);
System.out.println("Others: " + others);
}
}
```
运行这个程序,它会提示用户输入一行英文文本。然后,程序将遍历该字符串的每个字符,统计英文字母、数字和其他符号的数量,并输出结果。
希望这个程序对你有帮助!