gpt4 book ai didi

Java 编译器错误。 For 循环不是语句

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

所以我正在做一个作业,我们必须使用嵌套的 for 循环并传递参数来创建树的 ASCII 图片。我一生都无法弄清楚它没有编译是出了什么问题。

来源如下:

public class Tree{

public static void main(String [] args){

drawTree(3,4);
}

public static String drawTree(int n, int h){ //n is number of bodies, h is their height.

for(int i; i = 1; i <= h; i++){ //Number of tree bodies to create
drawSegment(n);
}

//Trunk
for(int i; i = 1; i <= h - 1; i++){ //Trunk spaces
System.out.print (" ");
}

System.out.print ("*"); //Trunk Body

for (int i; i = 1; i <= h - 1; i++){ //Trunk end spaces
System.out.print (" ");
}
}

//Leaves
public static String drawSegment(int k){ //Creates the tree's bodies

for(int i; i = 1; i <= k; i++){ //Number of lines

for(int s; s = 1; s <= k - 1; s++){ //Number of Spaces
System.out.print (" ");
}

for(int s; s <= (2 * i) - 1; s++){ //Number of Stars
System.out.print ("*");
}

for(int s; s = 1; s <= k - 1; s++){ //Number of End Spaces
System.out.println (" ");
}

}
}
}

这些是错误:

Tree.java:10: error: not a statement
for(int i; i = 1; i <= h; i++){ //Number of tree bodies to create
^
Tree.java:10: error: ')' expected
for(int i; i = 1; i <= h; i++){ //Number of tree bodies to create
^
Tree.java:10: error: ';' expected
for(int i; i = 1; i <= h; i++){ //Number of tree bodies to create
^
Tree.java:15: error: not a statement
for(int i; i = 1; i <= h - 1; i++){ //Trunk spaces
^
Tree.java:15: error: ')' expected
for(int i; i = 1; i <= h - 1; i++){ //Trunk spaces
^
Tree.java:15: error: ';' expected
for(int i; i = 1; i <= h - 1; i++){ //Trunk spaces
^
Tree.java:21: error: not a statement
for (int i; i = 1; i <= h - 1; i++){ //Trunk end spaces
^
Tree.java:21: error: ')' expected
for (int i; i = 1; i <= h - 1; i++){ //Trunk end spaces
^
Tree.java:21: error: ';' expected
for (int i; i = 1; i <= h - 1; i++){ //Trunk end spaces
^
Tree.java:28: error: not a statement
for(int i; i = 1; i <= k; i++){ //Number of lines
^
Tree.java:28: error: ')' expected
for(int i; i = 1; i <= k; i++){ //Number of lines
^
Tree.java:28: error: ';' expected
for(int i; i = 1; i <= k; i++){ //Number of lines
^
Tree.java:30: error: not a statement
for(int s; s = 1; s <= k - 1; s++){ //Number of Spaces
^
Tree.java:30: error: ')' expected
for(int s; s = 1; s <= k - 1; s++){ //Number of Spaces
^
Tree.java:30: error: ';' expected
for(int s; s = 1; s <= k - 1; s++){ //Number of Spaces
^
Tree.java:38: error: not a statement
for(int s; s = 1; s <= k - 1; s++){ //Number of End Spaces
^
Tree.java:38: error: ')' expected
for(int s; s = 1; s <= k - 1; s++){ //Number of End Spaces
^
Tree.java:38: error: ';' expected
for(int s; s = 1; s <= k - 1; s++){ //Number of End Spaces
^
18 errors

我认为这与第一个方法drawTree中的for循环无法找到h.但我不知道如何解决它。

最佳答案

替换

for(int i; i = 1; i <= h; i++)

与:

for(int i = 1; i <= h; i++)

关于Java 编译器错误。 For 循环不是语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22180088/

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