gpt4 book ai didi

java - 错误java。我不知道如何修复它

转载 作者:行者123 更新时间:2023-12-01 23:21:25 25 4
gpt4 key购买 nike

我正在学习 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

我该如何解决这个问题?

最佳答案

您将 ; 放在循环的末尾:

for (int i = 0; i < counts.length; i++);
^

去掉这些,并用 {} 包围循环体。

现在的问题是 i 仅存在于循环范围内。但是,您已通过添加 ; 终止了循环范围,因此当您在外部引用 i 时,您会收到编译错误。

关于java - 错误java。我不知道如何修复它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20571487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com