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

java統計字元

發布時間: 2023-07-15 11:06: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程序如何統計某字元在該字元串中出現的次數思路是什麼

思路:x0dx0a先輸入字元串與想要統計的字元,然後遍歷字元串的每一個字元,進行統計。x0dx0a代數虧芹碼:x0dx0aimportjava.text.ParseException;x0dx0aimportjava.util.Scanner;x0dx0ax0dx0aclassDemoAsm{x0dx0apublicstaticvoidmain(String[]args)throwsParseException{x0dx0aScannersc=newScanner(System.in);x0dx0a//輸入字元串x0dx0aStringstr=sc.nextLine();x0dx0a//輸入字元x0dx0aStringstr1=sc.nextLine();x0dx0ax0dx0a//遍歷字元薯畢串x0dx0aintsum=0;x0dx0afor(inti=0;i

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

}

}

❹ java統計串中字元個數的代碼解釋

整個代碼就是為了統計字元串中各個字元出現的個數
Map
result
=
getCharMaps(s);
這個方法調用
主要是看這個方法
public
static
Map
getCharMaps(String
s)
{
Map
map
=
new
HashMap();
//定義一個map
for(int
i
=
0;
i
<
s.length();
i++)
{
//
下面的代碼主要是循環字元串,一次取出每一個字元
Character
c
=
s.charAt(i);
//獲取對應的字元
Integer
count
=
map.get(c);
//獲取字元對應出現的次數
map.put(c,
count
==
null
?
1
:
count
+
1);
//如果之前出現過,則加1,否則直接賦1
}
return
map;
//返回最終的map
}

熱點內容
如何下載油猴腳本並安裝 發布:2025-02-08 15:02:12 瀏覽:594
硬體哪個配置性價比高 發布:2025-02-08 14:47:07 瀏覽:146
如何去掉僅限自動配置 發布:2025-02-08 14:37:55 瀏覽:708
壓縮空氣有啥 發布:2025-02-08 14:26:01 瀏覽:703
python輸入一個數 發布:2025-02-08 14:26:00 瀏覽:451
普惠e卡最初密碼是多少 發布:2025-02-08 14:21:57 瀏覽:476
亞索後q腳本 發布:2025-02-08 14:21:06 瀏覽:325
官方源碼 發布:2025-02-08 14:09:25 瀏覽:438
python過濾器 發布:2025-02-08 14:05:06 瀏覽:618
火山幣演算法 發布:2025-02-08 14:04:49 瀏覽:670