除了糟糕的格式之外,为什么不能将它们打印出来一次。我不认为我正确地将它存储在数组中。所以,如果你们可以帮我解决我的错误,我将不胜感激
import java.util.*;
public class LetCount {
public static final int NUMCHARS = 26 + 1; //You define
public static int addr(char ch) {
return (int) ch - (int) 'A' + 1;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in); // for reading input
int[] count = new int [NUMCHARS];
int curIndex = 0;
int max = 0;
System.out.println("Enter text to be read");
while (keyboard.hasNext()) {
String str1 = keyboard.nextLine().toUpperCase();
char ch = str1.charAt(curIndex);
for(curIndex = 0; curIndex < str1.length(); curIndex++) {
ch = str1.charAt(curIndex);
if (ch >= 'A' && ch <= 'Z') {
addr(ch);
count[ch - 'A']++;
}
for (int i = 0; i > 26; ++i) {
int highest;
highest = count[i];
System.out.printf(" %c ", i + 'A');
System.out.println("occurred " + count[i] + " times");
if (highest >= max) {
if (highest > max) {
max = highest;
}
}
}
System.out.println(max);
}
}
}
}
答案 0 :(得分:0)
您的代码中有几个问题:
for loop
读取此内容:for (int i = 0; i > 26; ++i) {
应该是for (int i = 0; i < 26; ++i) {
addr
功能。以下是更正后的代码:
import java.util.*;
public class LetCount {
public static final int NUMCHARS = 26 + 1; //You define
public static int addr(char ch) {
return (int) ch - (int) 'A' + 1;
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in); // for reading input
int[] count = new int [NUMCHARS];
int curIndex = 0;
int max = 0;
System.out.println("Enter text to be read");
while (keyboard.hasNext()) {
String str1 = keyboard.nextLine().toUpperCase();
char ch = str1.charAt(curIndex);
for(curIndex = 0; curIndex < str1.length(); curIndex++) {
ch = str1.charAt(curIndex);
if (ch >= 'A' && ch <= 'Z') {
// addr(ch); No need to use this
count[ch - 'A']++;
}
}
int highest;
for (int i = 0; i < 26; ++i) { //you had the < sign wrong here.
highest = count[i];
if (highest >= max) {
if (highest > max) {
max = highest;
}
}
}
//print the entire thing only once.
for (int i = 0; i < 26; ++i) {
System.out.printf(" %c ", i + 'A');
System.out.println("occurred " + count[i] + " times");
}
System.out.println("Maximum is " + max);
}
}
}