Java: 特定字符报告数
Java: Number of specific character reports
我编写了一个程序来计算短语中每个字母的数量
我想要return的结果文本是
Enter a phrase
"Cow goes to the market"
There are 1 C('s), 3 O('s), 1 W('s), 1 G('s), 3 E('s), 1 S('s), 3 T('s).. /* you get the gist */ in your phrase.
我希望将结果连接成一个字符串,而不仅仅是一个多行报告。
import java.util.*;
public class charCount
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
//Prompt for user entry
System.out.println("Enter a phrase: ");
String letterN = keyboard.nextLine();
charCount(letterN);
}
public static int charCount(String letterN)
{ //variable initializations
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
a = b = c = d = e = f = g = h = i = j = k = l = m = n = o = p = q = r = s = t = u = v = w = x = y = z = 0;
int total = 0;
total += 1;
//for loop
for(int countN = 0; countN < letterN.length(); countN++) {
switch(letterN.charAt(countN)) {
case 'a':
a++;
break;
case 'b':
b++;
break;
case 'c':
c++;
break;
case 'd':
d++;
break;
case 'e':
e++;
break;
case 'f':
f++;
break;
case 'g':
g++;
break;
case 'h':
h++;
break;
case 'i':
i++;
break;
case 'j':
j++;
break;
case 'k':
k++;
break;
case 'l':
l++;
break;
case 'm':
m++;
break;
case 'n':
n++;
break;
case 'o':
o++;
break;
case 'p':
p++;
break;
case 'q':
q++;
break;
case 'r':
r++;
break;
case 's':
s++;
break;
case 't':
t++;
break;
case 'u':
u++;
break;
case 'v':
v++;
break;
case 'w':
w++;
break;
case 'x':
x++;
break;
case 'y':
y++;
break;
case 'z':
z++;
break;
}
}
//Console logs
System.out.println("Below are the individual character counts for your string");
System.out.println("A: " + a);
System.out.println("B: " + b);
System.out.println("C: " + c);
System.out.println("D: " + d);
System.out.println("E: " + e);
System.out.println("F: " + f);
System.out.println("G: " + g);
System.out.println("H: " + h);
System.out.println("I: " + i);
System.out.println("J: " + j);
System.out.println("K: " + k);
System.out.println("L: " + l);
System.out.println("M: " + m);
System.out.println("N: " + n);
System.out.println("O: " + o);
System.out.println("P: " + p);
System.out.println("Q: " + q);
System.out.println("R: " + r);
System.out.println("S: " + s);
System.out.println("T: " + t);
System.out.println("U: " + u);
System.out.println("V: " + v);
System.out.println("W: " + w);
System.out.println("X: " + x);
System.out.println("Y: " + y);
System.out.println("Z: " + z);
return total;
}
}
我的问题是,我当然可以连接 System.out.println();使用 int vars 但它只是打印字母数。我事先不知道人们会输入什么,所以我需要我的程序来检测字母是否 = true,从而 returns 报告中的字符和数字。
我一直在寻找解决方案,但一无所获。我还不能完全理解这个问题,坦率地说,我不知道如何表达这个问题。
提前谢谢大家
用一个Map来存储你遇到过的人物,以及见过多少次。使用 TreeMap 意味着当您遍历地图时,条目将按自然顺序排序
import java.util.*;
public class CharCount {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//Prompt for user entry
System.out.println("Enter a phrase: ");
String letterN = keyboard.nextLine();
charCount(letterN);
}
public static void charCount(String letterN) {
Map<Character, Long> charMap = new TreeMap<>();
for (char aChar : letterN.toCharArray()) {
if (charMap.containsKey(aChar)) {
charMap.put(aChar, charMap.get(aChar) + 1);
}
else {
charMap.put(aChar, 1L);
}
// this if/else can be written using the ternary operator as:
// charMap.put(aChar, charMap.containsKey(aChar) ? charMap.get(aChar) + 1 : 1L);
}
for (Character character : charMap.keySet()) {
System.out.println(character + ": " + charMap.get(character));
}
}
}
我编写了一个程序来计算短语中每个字母的数量
我想要return的结果文本是
Enter a phrase
"Cow goes to the market"
There are 1 C('s), 3 O('s), 1 W('s), 1 G('s), 3 E('s), 1 S('s), 3 T('s).. /* you get the gist */ in your phrase.
我希望将结果连接成一个字符串,而不仅仅是一个多行报告。
import java.util.*;
public class charCount
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner (System.in);
//Prompt for user entry
System.out.println("Enter a phrase: ");
String letterN = keyboard.nextLine();
charCount(letterN);
}
public static int charCount(String letterN)
{ //variable initializations
int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z;
a = b = c = d = e = f = g = h = i = j = k = l = m = n = o = p = q = r = s = t = u = v = w = x = y = z = 0;
int total = 0;
total += 1;
//for loop
for(int countN = 0; countN < letterN.length(); countN++) {
switch(letterN.charAt(countN)) {
case 'a':
a++;
break;
case 'b':
b++;
break;
case 'c':
c++;
break;
case 'd':
d++;
break;
case 'e':
e++;
break;
case 'f':
f++;
break;
case 'g':
g++;
break;
case 'h':
h++;
break;
case 'i':
i++;
break;
case 'j':
j++;
break;
case 'k':
k++;
break;
case 'l':
l++;
break;
case 'm':
m++;
break;
case 'n':
n++;
break;
case 'o':
o++;
break;
case 'p':
p++;
break;
case 'q':
q++;
break;
case 'r':
r++;
break;
case 's':
s++;
break;
case 't':
t++;
break;
case 'u':
u++;
break;
case 'v':
v++;
break;
case 'w':
w++;
break;
case 'x':
x++;
break;
case 'y':
y++;
break;
case 'z':
z++;
break;
}
}
//Console logs
System.out.println("Below are the individual character counts for your string");
System.out.println("A: " + a);
System.out.println("B: " + b);
System.out.println("C: " + c);
System.out.println("D: " + d);
System.out.println("E: " + e);
System.out.println("F: " + f);
System.out.println("G: " + g);
System.out.println("H: " + h);
System.out.println("I: " + i);
System.out.println("J: " + j);
System.out.println("K: " + k);
System.out.println("L: " + l);
System.out.println("M: " + m);
System.out.println("N: " + n);
System.out.println("O: " + o);
System.out.println("P: " + p);
System.out.println("Q: " + q);
System.out.println("R: " + r);
System.out.println("S: " + s);
System.out.println("T: " + t);
System.out.println("U: " + u);
System.out.println("V: " + v);
System.out.println("W: " + w);
System.out.println("X: " + x);
System.out.println("Y: " + y);
System.out.println("Z: " + z);
return total;
}
}
我的问题是,我当然可以连接 System.out.println();使用 int vars 但它只是打印字母数。我事先不知道人们会输入什么,所以我需要我的程序来检测字母是否 = true,从而 returns 报告中的字符和数字。
我一直在寻找解决方案,但一无所获。我还不能完全理解这个问题,坦率地说,我不知道如何表达这个问题。
提前谢谢大家
用一个Map来存储你遇到过的人物,以及见过多少次。使用 TreeMap 意味着当您遍历地图时,条目将按自然顺序排序
import java.util.*;
public class CharCount {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
//Prompt for user entry
System.out.println("Enter a phrase: ");
String letterN = keyboard.nextLine();
charCount(letterN);
}
public static void charCount(String letterN) {
Map<Character, Long> charMap = new TreeMap<>();
for (char aChar : letterN.toCharArray()) {
if (charMap.containsKey(aChar)) {
charMap.put(aChar, charMap.get(aChar) + 1);
}
else {
charMap.put(aChar, 1L);
}
// this if/else can be written using the ternary operator as:
// charMap.put(aChar, charMap.containsKey(aChar) ? charMap.get(aChar) + 1 : 1L);
}
for (Character character : charMap.keySet()) {
System.out.println(character + ": " + charMap.get(character));
}
}
}