2014-03-04 69 views
-1

所以我正在做一個賦值,我們必須使用嵌套循環並傳遞參數來創建樹的ASCII圖片。我不能爲了我的生活找出它沒有編譯的問題。Java編譯器錯誤。 For循環不是一個語句

這裏的源:

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 

我認爲這事做的for循環的第一種方法drawTree未能找到小時。但我不知道如何解決它。

+0

改爲說'int i = 1'。 – devnull

+0

你可能想在亞馬遜上試用這本書。 Java初學者參考:Harvey Benson的快速參考我今天買了它,並且發現它充滿了有關Java的高質量代碼和事實。 –

+0

我有一本書(建設Java程序:回到基礎的方法),並正在上課。其實我真的很尷尬,我甚至在看到答案時問過這個問題。我已經爲循環寫了數百個循環,出於某種原因,有一個巨大的腦部放屁做這個任務,並忘記了部分語法。這是一個新手的錯誤,它在內部傷害了我。謝謝大家的答案。 – Reeses

回答

3

更換

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

有了:

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

有待

for(int i = 1; i <= h - 1; i++) {} 

如此反覆的for循環

1

其餘在每一個for循環您這樣做:

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

,但它應該是:

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

只有兩個; ;

for(init;condition;statement)