用java编程统计用户从键盘输入的字符串中所包含的字母,数字和其他字符串的个数?
3个回答
展开全部
import java.util.Scanner;
public class Test {
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.print("输入字符串:");
String strs = input.next();
int number = 0;
int chara = 0;
int other = 0;
char [] chs = strs.toCharArray();
for(char c : chs){
if(c >= '0' && c <= '9'){
number++;
}else if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' ){
chara++;
}else{
other++;
}
}
System.out.println("数字有:" + number + "个,字符有" + chara + "个,其他有:" + other + "个。" );
}
}
展开全部
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _1 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String zifuchuan = new String("");
int hanzishu = 0;
int zimu = 0;
int kongge = 0;
int shuzi = 0;
int qita = 0;
System.out.print("请输入一行字符:");
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
zifuchuan = stdin.readLine();
byte[] bytes = zifuchuan.getBytes();
for (int i = 0; i < bytes.length; i++) {
if ((bytes[i] >= 65 && bytes[i] <= 90)
|| (bytes[i] >= 97 && bytes[i] <= 122))
zimu++;
else if (bytes[i] == 32)
kongge++;
else if (bytes[i] >= 48 && bytes[i] <= 57)
shuzi++;
else if (bytes[i] < 0)
hanzishu++;
else
qita++;
}
System.out.println("字符串所占字节个数为:" + bytes.length);
System.out.println("汉字个数为:" + hanzishu / 2);
System.out.println("英文字母个数为:" + zimu);
System.out.println("空格个数为:" + kongge);
System.out.println("数字个数为:" + shuzi);
System.out.println("其他字符个数为:" + qita);
}
}
import java.io.IOException;
import java.io.InputStreamReader;
public class _1 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String zifuchuan = new String("");
int hanzishu = 0;
int zimu = 0;
int kongge = 0;
int shuzi = 0;
int qita = 0;
System.out.print("请输入一行字符:");
BufferedReader stdin = new BufferedReader(new InputStreamReader(
System.in));
zifuchuan = stdin.readLine();
byte[] bytes = zifuchuan.getBytes();
for (int i = 0; i < bytes.length; i++) {
if ((bytes[i] >= 65 && bytes[i] <= 90)
|| (bytes[i] >= 97 && bytes[i] <= 122))
zimu++;
else if (bytes[i] == 32)
kongge++;
else if (bytes[i] >= 48 && bytes[i] <= 57)
shuzi++;
else if (bytes[i] < 0)
hanzishu++;
else
qita++;
}
System.out.println("字符串所占字节个数为:" + bytes.length);
System.out.println("汉字个数为:" + hanzishu / 2);
System.out.println("英文字母个数为:" + zimu);
System.out.println("空格个数为:" + kongge);
System.out.println("数字个数为:" + shuzi);
System.out.println("其他字符个数为:" + qita);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
很简单的 用for循环遍历判断
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询