gpt4 book ai didi

Java While 循环语法错误?

转载 作者:行者123 更新时间:2023-12-01 18:33:39 24 4
gpt4 key购买 nike

我正在尝试一个形状制作 Java 项目,但我的 while 循环中不断出现语法错误,我无法弄清楚 - 有什么建议吗?

这是迄今为止我的代码:

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);

System.out.println("Hello! I am a program that draws shapes, but YOU must tell me how big you wish me to make them!");
System.out.println("Enter shape size: ");
final int MAX_ROWS = scan.nextInt();

// DIAMOND
int spaces = 0, stars, row;

for (row = 1; row <= (MAX_ROWS + 1) / 2; row++) {
for (spaces = 0; spaces < MAX_ROWS - row; spaces++) {
System.out.print(" ");
}
for (stars = 0; stars < row; stars++) {
System.out.print("* ");
}
System.out.println("");
}

for (row = ((MAX_ROWS + 1) / 2); row < MAX_ROWS; row++) {
for (spaces = 1; spaces < row; spaces++) {
System.out.print(" ");
}
for (stars = 0; stars < MAX_ROWS - row; stars++) {
System.out.print(" *");
}
System.out.println("");
}
System.out.println("Diamond, size " + MAX_ROWS + ".");

}

// HOLLOW SQUARE

int loopVariable = 0;

private static final int MAX_ROWS

while(loopVariable < MAX_ROWS) { <--- THIS IS THE SYNTAX ERROR LINE
if(loopVariable == MAX_ROWS - 1 ||
loopVariable == 0) {

for(int x=0; x < MAX_ROWS; x++) {
System.out.print("* ");
}
} else {
for(int x=0; x < MAX_ROWS; x++ ) {
if(x == 0||x == MAX_ROWS-1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
}
System.out.println();
System.out.println("Hollow Square, size " + MAX_ROWS + ".");

loopVariable++;
}

}

这些是它给我的错误:

该行有多个标记 - 标记“while”上的语法错误,=预期 - 语法错误,请插入“;”去完成 字段声明

提前致谢!

最佳答案

您没有在上一行中添加分号。错误不在 while 循环行上,但编译器仅在到达该行时才注意到错误。

插入一个“;”在 private static Final int MAX_ROWS 之后,它应该可以工作。

编辑:如果您打算稍后在代码中更改“MAX_ROWS”的值,您还需要从声明中删除“final”。您无法更改“最终”值。此外,正如有人提到的,您需要对其进行初始化,例如将其设置为 0。您可以稍后更改它。

关于Java While 循环语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23002502/

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