【发布时间】:2014-04-11 02:35:13
【问题描述】:
我正在尝试一个塑造形状的 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 循环之前的行之后。 -
它在偏僻的地方。
标签: java syntax while-loop syntax-error nested-loops