所以我正在进行一项任务,我们必须使用嵌套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有关。但我不知道如何解决它。
答案 0 :(得分:4)
替换
for(int i; i = 1; i <= h; i++)
使用:
for(int i = 1; i <= h; i++)
答案 1 :(得分:2)
需要
for(int i = 1; i <= h - 1; i++) {}
依此类推for for for循环
答案 2 :(得分:1)
在每个for循环中,您执行此操作:
for(int i; i = 1; i <= h; i++){
但它应该是:
for(int i = 1; i <= h; i++){
只有两个; ;
for(init;condition;statement)
答案 3 :(得分:0)
使用
for(int i = 1; i <= h; i++)
{
Your Code.
}
注意:这是FOR LOOP的规则 for(初始化;条件;声明)