我正在學習java,並且存在此代碼的錯誤。我根本不知道如何解決它。Bug java。我不知道如何修復它
下面的代碼:
public class CountLettersInArray {
public static void main(String[] args) {
char[] chars = createArray();
System.out.println("The lowercase letters are:");
displayArray(chars);
int[] counts = countLetters(chars);
System.out.println(" ");
System.out.println("The occurence of each letter are: ");
displayCounts(counts);
}
public static void displayCounts(int[] counts) {
for (int i = 0; i < counts.length; i++);
if ((i + 1) % 10 == 0);
System.out.println(counts[i] + " " + (char)(i + 'a'));
else
System.out.println(counts[i] + " " + (char)(i + 'a') + " ");
}
public static int[] countLetters(char[] chars) {
//Declare and create an array of 26 int
int[] counts = new int[26];
//For each lowercase letter in the array, count it
for (int i = 0; i < chars.length; i++);
counts[chars[i] - 'a']++;
return counts;
}
public static void displayArray(char[] chars) {
//Display the characters in the array 20/line
for (int i = 0; i < chars.length; i++);
if ((i + 1) % 20 == 0)
System.out.println(chars[i]);
else
System.out.print(chars[i] + " ");
}
public static char[] createArray() {
//Declare the array of characters and create it
char[] chars = new char[100];
//Create lowercase characters randomly and assign them to array
for (int i = 0; i < chars.length; i++);
chars[i] = RamdomCharacter.getRandomLowerCaseLetter();
//This return the array
return chars;
}
}
我與Eclypse編碼它和軟件告訴我,這兩件事情:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
i cannot be resolved to a variable
RamdomCharacter cannot be resolved
我怎麼supose解決這一問題?
正如Eclipse所說,RamdomCharacter類不存在。如果你在你的項目中有它,你必須將它導入到這個類的頂部。而你的每一個必須寫成這樣:for(...){actions; } – Kloe2378231
請在Eclipse中不時按下「Ctrl + Shift + F」來查看你的代碼的真實外觀(例如,這會顯示你在'for(..)'後面有';')。另外什麼是「RamdomCharacter」?是否有可能要訪問* Ra ** n ** domCharacter *? – Pshemo
學習如何爲您的循環和if/else結構使用大括號('{'和'}')。 – GriffeyDog