编写java程序:输入一个字符串,判断有几个英文字母,有几个数字,有几个其它的字符 用java编写一个程序,接收用户输入的字符串,输出其中英文字...
Java\u7f16\u7a0b\uff1a \u7528\u6237\u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5224\u65ad\u6709\u51e0\u4e2a\u82f1\u6587\u5b57\u6bcd\uff0c\u6709\u51e0\u4e2a\u6570\u5b57\uff0c\u6709\u51e0\u4e2a\u5176\u5b83\u7684\u5b57\u7b26\u3002\u521d\u59cb\u5316\u4e09\u4e2a\u4e3a0\u7684\u53d8\u91cf \u5206\u522b\u4ee3\u8868\u5b57\u6bcd\u3001\u6570\u5b57\u3001\u5176\u4ed6\u5b57\u7b26\u7684\u6570\u91cf \u5faa\u73af\u904d\u5386\u6bcf\u4e2a\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26 \u4f9d\u6b21\u5224\u65ad \u5b57\u6bcd\u7684\u8bdd\u5b57\u6bcd\u90a3\u4e2a\u53d8\u91cf++ \u3002\u3002\u3002\u5176\u4ed6\u540c\u7406
importjava.util.Scanner;publicclassA04{publicstaticvoidmain(Stringargs[]){System.out.println("\u8bf7\u8f93\u5165\u5b57\u7b26\u4e32\uff1a");Strings=newScanner(System.in).next();System.out.println("\u8f93\u51fa\u7ed3\u679c\uff1a");for(charc:s.toCharArray()){if(c>='a'&&c='A'&&c<='Z'){c+=32;}System.out.print(c);}}}
public class Main {public static void main(String args[]){
String str1="abfdTE1879!!";//可以从控制台输入
String str2=str1.replaceAll("[a-z|A-Z]","");
System.out.println("英文字符的个数为"+(str1.length()-str2.length()));
str1=str2;
str2=str1.replaceAll("[0-9]","");
System.out.println("数字字符的个数为"+(str1.length()-str2.length()));
System.out.println("其它字符的个数为"+str2.length());
}
}
这是最简洁的
运行示例:
英文字符的个数为6
数字字符的个数为4
其它字符的个数为2
public static void main(String[] args) {
int count_abc=0,count_num=0,count_oth=0;
//输入一串数
Scanner scan=new Scanner(System.in);
String str = scan.next();
char[] chars = str.toCharArray();
//判断每个字符
for(int i = 0;i<chars.length;i++){
if((chars[i]>=65&&chars[i]<=90)||(chars[i]>=97&&chars[i]<=122)){
count_abc++;
}else if(chars[i]>=48&&chars[i]<=57){
count_num++;
}else{
count_oth++;
}
}
System.out.println("字母有:"+count_abc+"个");
System.out.println("数字有:"+count_num+"个");
System.out.println("其他的有:"+count_oth+"个");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int abcCount=0;//英文字母个数
int spaceCount=0;//空格键个数
int numCount=0;//数字个数
int otherCount=0;//其他字符个数
Scanner scan=new Scanner(System.in);
String str=scan.nextLine();
char[] ch = str.toCharArray();
for(int i=0;i<ch.length;i++){
if(Character.isLetter(ch[i])){
//判断是否字母
abcCount++;
}
else if(Character.isDigit(ch[i])){
//判断是否数字
numCount++;
}
else if(Character.isSpaceChar(ch[i])){
//判断是否空格键
spaceCount++;
}
else{
//以上都不是则认为是其他字符
otherCount++;
}
}
System.out.println("字母个数:"+abcCount);
System.out.println("数字个数:"+numCount);
System.out.println("空格个数:"+spaceCount);
System.out.println("其他字符个数:"+otherCount);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String getMess=scan.nextLine();
int zm=0;
int num=0;
int other=0;
char [] ch = getMess.toCharArray();
for(int i=0;i<ch.length;i++){
if((ch[i]>='a' && ch[i]<='z') || (ch[i]>='A' && ch[i]<='Z')){
zm=zm+1;
}else if(ch[i]>47 && ch[i]<58){
num=num+1;
}else{
other=other+1;
}
}
System.out.println("字母:"+zm);
System.out.println("数字:"+num);
System.out.println("其它:"+other);
}
绛旓細{ while (true){ try//---鐢╥o搴撳氨涓瀹氳鍔犱笂寮傚父澶勭悊 { BufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.println("璇杈撳叆涓涓瓧绗︿覆锛");string s = br.readLine();//readLine()璇诲叆绋嬪簭涓殑鏄竴涓瓧绗︿覆銆//java璨屼技浠庢帶鍒跺彴璇诲叆鐨勯兘鏄瓧绗︿覆锛屼竴鑸...
绛旓細System.out.println("璇杈撳叆锛");String str=sn.next();boolean isWord=false;for(int i=0;i<ch1.length;i++){ if(str.equals(ch1[i])){ System.out.println("灏忓啓澹版瘝锛"+str);isWord=true;break;} } for(int i=0;i<ch11.length;i++){ if(str.equals(ch11[i])){ System....
绛旓細++space;}else if(value>=48 && value<=57){ ++num;}else if((value>=65 && value<=90) || (value>=97 && value<=122) ){ ++letter;}else{ ++other;} } System.out.println("鏁板瓧涓暟锛"+num+"\n瀛楁瘝涓暟锛"+letter+"\n绌烘牸涓暟锛"+space+"\n鍏朵粬瀛楃涓暟锛"+other);...
绛旓細//鐢ㄤ簬淇濆瓨鍏冮煶瀛楁瘝鐨勪釜鏁帮紙涓嶅尯鍒嗗ぇ灏忓啓锛塮or (int i = 0; i < str.length(); i++) {char c = str.charAt(i);for (int j = 0; j < cs.length; j++) {if(c==cs[j]){arys[j]++;//灏忓啓鍏冮煶涓暟澧炲姞sumx++;break;}}for (int j = 0; j < bcs.length; j++) {if(c...
绛旓細// 寮曞寘 import java.util.Scanner;public static void main(String[] args) { Scanner input = new Scanner(System.in); // 鎺у埗鍙拌緭鍏ョ被 System.out.print("璇杈撳叆涓涓瓧绗[ 濡 a ]锛"); // 鎻愮ず杈撳叆 String word = input.next(); // 杈撳叆 if (word.length() == 1) { // 濡傛灉...
绛旓細鍙傝Java婧愪唬鐮侊細import java.util.Scanner;public class Test { public static void main(String[] args) { Scanner scan = new Scanner(System.in);char ch;System.out.print("input a character: ");ch = scan.next().charAt(0);System.out.printf("previor two: %c %c\n", (char)(...
绛旓細// 銆愪唬鐮併//鎻愬彇鐢ㄦ埛杈撳叆鐨瀛楃涓璧嬪肩粰str str = reader.nextLine();// 銆愪唬鐮併//涔﹀啓switch璇彞鐢ㄦ潵缁熻瀛楃涓暟 switch (grade) { case 'A':aCount++;break;case 'B':bCount++;break;case 'C':cCount++;break;case 'D':dCount++;break;} ...
绛旓細importjava.util.Scanner;publicclassA04{publicstaticvoidmain(Stringargs[]){System.out.println("璇杈撳叆瀛楃涓诧細");Strings=newScanner(System.in).next();System.out.println("杈撳嚭缁撴灉锛");for(charc:s.toCharArray()){if(c>='a'&&c='A'&&c<='Z'){c+=32;}System.out.print(c);}}...
绛旓細杩囦簡寰堜箙鎵嶇湅鍒颁綘鐨勬彁闂紝涓嶅ソ鎰忔濓紝甯屾湜瀵逛綘鏈夊府鍔╋細import java.util.Scanner;public class IoTest { public static void main(String[] args) { Scanner input = new Scanner(System.in);String str1 = input.nextLine();//杈撳叆涓涓瓧绗︿覆 String str2 = input.nextLine();//鎸囧畾瀛楃 String...
绛旓細package test;import java.util.Scanner;public class test {public static void main(String[] args) {while(true){Scanner input=new Scanner(System.in);System.out.print("璇杈撳叆甯﹀瓧姣嶇殑瀛楃涓诧細");String str1=input.next(); int counA=0;int couna=0; for(int i=0;i<str1.le...