[英]How to count multiple character occurrences in a string using the switch loop? Java
想要計算字母a,e,s,t也是大寫字母。 使用開關循環。 我已經擁有它所以它計算空白但我不知道如何設置開關以及如何打印它。 謝謝
import java.util.Scanner;
public class Count {
public static void main(String[] args) {
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int length; // the length of the phrase
char ch; // an individual character in the string
Scanner scan = new Scanner(System.in);
// Print a program header
System.out.println();
System.out.println("Character Counter");
System.out.println();
// Read in a string and find its length
System.out.print("Enter a sentence or phrase: ");
phrase = scan.nextLine();
length = phrase.length();
// Initialize counts
countBlank = 0;
// a for loop to go through the string character by character
// and count the blank spaces
// Print the results System.out.println ();
for (int i = 0; i < phrase.length(); i++) {
if (Character.isWhitespace(phrase.charAt(i))) {
countBlank++;
}
}
ch = phrase.charAt(phrase.length());
System.out.println();
System.out.println("Number of blank spaces: " + countBlank);
System.out.println();
}
}
我認為這可能會有所幫助,
import java.util.Scanner;
class Count {
public static void main(String[] args) {
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int length; // the length of the phrase
char ch; // an individual character in the string
phrase = "abcd efgh skjadh sjds";
length = phrase.length();
int i = 0;
// Initialize counts
countBlank = 0;
// a for loop to go through the string character by character
// and count the blank spaces
// Print the results System.out.println ();
int aCount = 0;
int eCount = 0;
int sCount = 0;
int tCount = 0;
int capACount = 0;
int capECount = 0;
int capSCount = 0;
int capTCount = 0;
while(i < length) {
ch = phrase.charAt(i);
switch(ch) {
case 'a' :
aCount++;
break;
case 'e' :
eCount++;
break;
case 's' :
sCount++;
break;
case 't' :
tCount++;
break;
case 'A' :
capACount++;
break;
case 'E' :
capECount++;
break;
case 'S' :
capSCount++;
break;
case 'T' :
capTCount++;
break;
}
i++;
}
System.out.println("Number of a: " + aCount);
System.out.println("Number of e: " + eCount);
System.out.println("Number of s: " + sCount);
System.out.println("Number of t: " + tCount);
System.out.println("Number of A: " + capACount);
System.out.println("Number of E: " + capECount);
System.out.println("Number of S: " + capSCount);
System.out.println("Number of T: " + capTCount);
}
}
如何使用JDK代替:
int countBlank = phrase.length() - phrase.replace(" ", "").length();
int countAest = phrase.length() - phrase.replaceAll("(?i)[aest]", "").length();
這兩行的工作方式是刪除感興趣的字符,然后使用長度的變化來計算。
僅供參考,在正則表達式中, (?i)
表示“忽略大小寫”, [aest]
表示“任何字符a,e,s或t”。
考慮將要查找的String
也放在String
。 這樣,您可以對輸入phrase
使用charAt()
方法以及要查找的字符串。
public static void main(String[] args) {
// Print a program header
System.out.println();
System.out.println("Character Counter");
System.out.println();
// Read in a string and find its length
System.out.print("Enter a sentence or phrase: ");
Scanner scan = new Scanner(System.in);
String phrase = scan.nextLine();
int length = phrase.length();
// Initialize counts
String findChars = " AaEeSsTt"; // we will count each of these characters
int[] counts = new int[findChars.length()]; // the counts of findChars
// Go through the string character by character and count findChars
for (int i = 0; i < length; i++) {
char ch = phrase.charAt(i);
for (int n = 0; n < findChars.length(); n++) {
char find = findChars.charAt(n);
if (ch == find) {
counts[n]++;
break;
}
}
}
System.out.println();
// Print the results to System.out
for (int n = 0; n < findChars.length(); n++) {
System.out.println("Number of '"
+ findChars.charAt(n) + "': " + counts[n]);
}
System.out.println();
}
在下面的例子中,首先我們將一個句子作為String
,我們將讀取Character
by Character
,當我們讀取每個Character
我們將增加(相應的索引值),對於int Array
,如:
int value = ( int ) sentence.charAt (i);
characterCount [value]++;
如果只知道您讀取ASCII
字符集,那么您也可以使用這種方法,或者修改它以滿足項目的需要。
public class AlphabetCountingExample {
private static final int TOTAL_ASCII_CHARACTERS = 256;
private String sentence;
private int [] characterCount;
public AlphabetCountingExample () {
characterCount = new int [TOTAL_ASCII_CHARACTERS];
sentence = "Hello there!!!";
}
private void performTask () {
countCharacters ();
displayCount ();
}
private void countCharacters () {
for ( int i = 0; i < sentence.length (); ++i ) {
int value = ( int ) sentence.charAt (i);
characterCount [value]++;
}
}
private void displayCount () {
for ( int i = 0; i < characterCount.length; ++i ) {
if ( characterCount [ i ] > 0 ) {
System.out.println ( "Character: " + (char) i +
" Count: " + characterCount [ i ] );
}
}
}
public static void main (String[] args) {
new AlphabetCountingExample ().performTask ();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.