導航:首頁 > 編程語言 > java統計計數

java統計計數

發布時間:2023-03-06 18:07:18

java怎麼統計指定字元出現的次數。要用Scanner輸入一個字元

你輸入一個字元串Scanner方式,比如 string s1 =null;
Scanner scanner =new Scanner(System.in);
s1 = scanner.nextline;
然後把這個s1 tochararray 一下,變成一個char[] 數組
然後遍歷這個數組,比如你要找a出現的次數,你就設定一個count來計算。
遍歷數組時候,若char[i]="a" count就+1,
最後count就是統計的次數啦。

public class Jjjj {
/**
* @param args
*/
public static void main(String[] args) {
String s1 = (new Scanner(System.in)).next();
char[] char1 = s1.toCharArray();
int count = 0;
char a = "a".charAt(0);
for(int i=0;i<char1.length;i++){
if(char1[i] == a){
count++;
}
}
System.out.println(count);
}
}

輸入aaahfhkshka
統計a的個數 就是4

② java中怎麼統計輸入的次數

總體思路,定義一個靜態全局變數來統計方法執行次數,每進方法一次,統計次數加1
所有方法執行完成時,輸出統計次數就可以了。
示例代碼如下:
public class CountTest {
public static int count1 = 0;
public static int count2 = 0;
public static void main(String[] args) {
Random r = new Random();
for (int i=0; i < 10; i++) {
int num = r.nextInt();
if (num > 0.5) {
method1();
} else {
method2();
}
}

System.out.println(count1 + " " + count2);
}

public static void method1() {

③ java怎麼統計一個字元的個數字

int i;
06
/***countDigit統計數字的數目
07
* countLetter統計字母的數目
08
* countLetter統計其餘的數目
09
*/
10
int countDigit=0, countLetter=0, countOthers=0;
11

12
String input = 「dfdfdf23dfdjk8989」;
13

14
//將字元串變數轉化為字元數組
15
char[] charArray = input.toCharArray();
16
for(i=0;i<charArray.length;i++)
17
{
18
//ASIIC碼
19
if(charArray[i]<='z'&&charArray[i]>='a' || charArray[i]<='Z'&&charArray[i]>='A')
20
countLetter++;
21
else if(charArray[i]<='9' && charArray[i]>='0' )
22
countDigit++;
23
else
24
countOthers++;
25
}
26
System.out.println("the number of letter "+countLetter);
27
System.out.println("the number of digit "+countDigit);

④ 編寫java程序統計字元個數。

inteng=0;
intspace=0;
intnum=0;
intother=0;
Scannersc=newScanner(System.in);
System.out.println("請輸入");
Stringstr=sc.next();
char[]arr=str.charAt();
for(inti=0;i<arr.length;i++){
if((arr[i]>='A'&&arr[i]<='Z')||(arr[i]>='a'&&arr[i]<='z')){
eng++;
}elseif(arr[i]==''){
space++;
}elseif(arr[i]>='0'&&arr[i]<='9'){
num++;
}else{
other++;
}
}
System.out.println("英語字母個數:"+eng);
System.out.println("空格個數:"+space);
System.out.println("數字個數:"+num);
System.out.println("其他個數:"+other);

⑤ java 統計某個文件夾下所包含的文件和文件夾的個數

listFiles方法返回當前目錄內所有文件和子目錄的File對象數組。
獲取其大小即文件和子目錄的數量總和。

遍歷數組,用isDirectory方法檢驗元素是否為目錄,這樣就把子目錄數出來了。

做差就是文件的數量了。

哦,你是想繼續數子目錄的文件數量情況嗎?那就再最外面套個遞歸。

閱讀全文

與java統計計數相關的資料

熱點內容
為什麼代理伺服器沒有網關 瀏覽:843
車太賢主演電影一個女的死了 瀏覽:88
台灣表屍電影 瀏覽:758
邊洗澡邊做的韓國電影有哪些 瀏覽:268
羅伯特齊默爾曼的愛情在線觀看 瀏覽:109
韓國電影家教高級課程女老師是誰 瀏覽:468
主角叫秦狩電影 瀏覽:168
租的伺服器流量費怎麼算 瀏覽:299
男同色電影 瀏覽:872
言情小說農村劇 瀏覽:625
韓劇里叫秀珍與東哲 瀏覽:404
歐美青少年涉毒的劇 瀏覽:40
重生帶肉寫得好的小說作者 瀏覽:43
java在線excel 瀏覽:287
一部韓國電影下雨天在舞台上被人強奸 瀏覽:410
使用遠程伺服器編譯 瀏覽:834
無需下載可以直接觀看 瀏覽:976
有沒有那種男男電影強制愛的 瀏覽:118
金沙紫荊電影城今日影訊 瀏覽:112
看歐美片的網站 瀏覽:858