编写程序,输入一个字符 ch ,判断并输出字符的类型,即字母(alpha),数字(numeric),或其他字符(other).

如题所述

第1个回答  2013-03-27
importjava.util.Scanner;
public class Ex7 {

/**
* @param args
*/
public static void main(String[]args) {
// TODOAuto-generated method stub
int alpha = 0;//英文字母个数
int numeric = 0;//数字个数
int other = 0;//其他字符个数
Scanner in = new Scanner(System.in);
System.out.println("输入一行字符");
String str =in.nextLine();
char[] ch = str.toCharArray();//作用:将字符串拆分为字符到数组。
for (int i = 0; i < ch.length;i++) {
if (Character.isLetter(ch[i])) {
// 判断是否字母
alpha++;
} else if (Character.isDigit(ch[i])) {
// 判断是否数字
numeric++;
}
else {
// 以上都不是则认为是其他字符
other++;
}
}
System.out.println("字母个数:" +alpha);
System.out.println("数字个数:" +numeric);
System.out.println("其他字符个数:" +other);
}}
第2个回答  推荐于2018-02-27
#include <stdio.h>
void main()
{
char ch;
scanf("%c",&ch);
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
printf("%c is alpha\n",ch);
else if(ch>='0'&&ch<='9')
printf("%c is numeric\n",ch);
else
printf("%c is other\n",ch);
}本回答被提问者和网友采纳
相似回答