gpt4 book ai didi

java - 带有数组的标记存在语法错误

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

我是 Java 编码新手。我正在尝试编写一个程序来计算数字 13 的前 400 个倍数并将它们存储在一个整数数组中。我找不到为什么这个类有两个错误,我认为我没有犯错误..有人可以帮忙吗?第一个错误(这一行有三个错误)位于

System.out.println("the first 400 multiples of 13:" );

Syntax error on token ";", { expected after this token
Syntax error on token ""the first 400 multiples of 13:"", delete this token
Syntax error on token(s), misplaced construct(s)

第二个在最后一个 }

上面写着:

Syntax error, insert "}" to complete ClassBody

public class multiples_of_13 {
int[] thirteens = new int[400];
int numFound = 0;
// candidate: the number that might be a multiple
// of 13
int candidate = 1;

System.out.println("the first 400 multiples of 13:" );

while (numFound < 400) {
if (candidate % 13 == 0) {
thirteens[numFound] = candidate;
numFound++;
}
candidate++;
}
System.out.println("First 400 multiples of 13:");
for (int i = 0; i < 400; i++) {
System.out.print(thirteens[i] + " ");
}
}

最佳答案

您需要将代码放入 main 方法中,因为它是程序的入口。

就此而言,指令不允许在类的主体内,但在其方法或 block 之一内。

<小时/>

解决方案

public class multiples_of_13 {
public static void main(String[] args) {
int[] thirteens = new int[400];
int numFound = 0;
// candidate: the number that might be a multiple
// of 13
int candidate = 1;

System.out.println("the first 400 multiples of 13:" );

while (numFound < 400) {
if (candidate % 13 == 0) {
thirteens[numFound] = candidate;
numFound++;
}
candidate++;
}
System.out.println("First 400 multiples of 13:");
for (int i = 0; i < 400; i++) {
System.out.print(thirteens[i] + " ");
}
}
}

关于java - 带有数组的标记存在语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34265622/

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