當前位置:首頁 » 編程語言 » 統計字元個數java

統計字元個數java

發布時間: 2024-08-07 22:03:08

『壹』 編寫java程序統計字元個數。

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);

『貳』 java怎麼實現統計一個字元串中字元出現的次數

可陵首迅以用String的indexof(str,fromindex)方法,循環遍歷加一個計數器統計次數。

publicclassCountTimes{

publicstaticvoidmain(String[]args){

Stringstr="Intheentireworldthere'";

inttimes=searchstr("my",str);//返回2

System.out.println(times);

}

publicstaticintsearchstr(Stringkey,Stringstr){

int尺此index=0;//每次的搜索到的下標

intcount=0;//計數器
while((index=str.indexOf(key,芹啟index))!=-1){

index=index+key.length();

count++;
}
returncount;

}

}

『叄』 鐢╦ava 緙栧啓涓涓紼嬪簭錛屾帴鍙楃敤鎴瘋緭鍏ョ殑涓孌佃嫳鏂囨枃瀛楋紝緇熻″嚭鍏朵腑鐨勫瓧絎︿釜鏁般佸崟璇嶄釜鏁板拰鍙ュ瓙鐨勪釜鏁般

public static void main(String[] args)

{

System.out.println("璇瘋緭鍏ヨ嫳璇鐗囨,浠';'緇撴潫:");

Scannerscanner = newScanner(System.in);

String str = "";

int dc = 0;

int zc = 0;

intjc= 0;

while(scanner.hasNext())

{

str = scanner.next();

zc += str.length();

dc++;

System.out.println(str);

if (str.contains("."))

{

jc+= 1;

}

if (str.contains(";"))

{

break;

}

}

// scanner.

System.out.println("鍗曡瘝涓鏁頒負:" + dc);

System.out.println("瀛楁瘝鏁頒負錛" + (zc - 1));

System.out.println("鍙ユ暟涓:" +jc);

}

鎵╁睍璧勬枡錛

import java.io.*;

public class test

{ public static void main(String[] args) throws IOException

{ System.out.print("璇瘋緭鍏ヤ竴涓插瓧姣:");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in))錛

String str=br.readLine();

int char1=0,letter=0,sentence=0;

for(int i=0;i<str.length();i++)

{

char ch = str.charAt(i);

if(ch!=' ')

{ char1++;

}

if(ch==' '||ch=='/n')

{ //鏈夌┖鏍,鍔犱竴鍗曡瘝

if(i!=0&&str.charAt(i-1)!=' ')

letter++;

}

if(ch=='?'||ch=='!'||ch=='.')

{ sentence++;

}

}

System.out.println("瀛楃︽暟錛"+char1);

System.out.println("鍗曡瘝涓鏁:"+letter);

System.out.println("鍙ュ瓙鏁:"+sentence)錛

}

}

import java.io.*;

public class test

{ public static void main(String[] args) throws IOException

{ System.out.print("璇瘋緭鍏ヤ竴涓插瓧姣:");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str=br.readLine();

int char1=0,letter=0,sentence=0;

for(int i=0;i<str.length();i++)

{

char ch = str.charAt(i);

if(ch!=' ')

{ char1++;

}

if(ch==' '||ch=='/n')

{ //鏈夌┖鏍,鍔犱竴鍗曡瘝

if(i!=0&&str.charAt(i-1)!=' ')

letter++;

}

if(ch=='?'||ch=='!'||ch=='.')

{ sentence++;

}

System.out.println("瀛楃︽暟錛"+char1);

System.out.println("鍗曡瘝涓鏁:"+letter);

System.out.println("鍙ュ瓙鏁:"+sentence);

}

}

熱點內容
為啥編程廣告 發布:2025-07-04 07:30:01 瀏覽:567
資料庫備機 發布:2025-07-04 07:30:00 瀏覽:530
靜態內部類java 發布:2025-07-04 07:25:45 瀏覽:232
玉林電信dns伺服器地址 發布:2025-07-04 07:17:34 瀏覽:437
用鏡像壓縮 發布:2025-07-04 07:17:31 瀏覽:635
lgg3如何設置鎖屏密碼 發布:2025-07-04 06:41:39 瀏覽:346
sqlserver2005命令 發布:2025-07-04 06:27:08 瀏覽:734
壓縮文件啥樣 發布:2025-07-04 06:21:42 瀏覽:300
linux的回車換行 發布:2025-07-04 06:21:39 瀏覽:134
c語言的窗口 發布:2025-07-04 06:20:54 瀏覽:985