導航:首頁 > 編程語言 > 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統計計數相關的資料

熱點內容
肌肉塑造全書pdf下載 瀏覽:796
安卓簡約拼圖用什麼軟體好 瀏覽:289
fx1n加密程序 瀏覽:844
淘客阿里雲伺服器 瀏覽:476
100壓縮打造 瀏覽:422
安卓手機怎麼和蘋果平板傳文件 瀏覽:973
開始選項卡中的頁眉和頁腳命令選項 瀏覽:424
pdf的字體怎麼改 瀏覽:856
python讀寫視頻 瀏覽:88
科魯茲壓縮機軸承 瀏覽:353
word文檔轉換成pdf文件找不到 瀏覽:27
組件注冊命令 瀏覽:760
安卓大屏導航用的是什麼運放 瀏覽:443
myandroidtools的備份 瀏覽:900
python爬蟲天氣預報 瀏覽:761
android70許可權管理 瀏覽:749
魔獸辛迪加是什麼伺服器 瀏覽:472
電腦文件夾排序怎麼自定義排序 瀏覽:41
android70機型 瀏覽:422
阿里程序員地位 瀏覽:183