2015-12-13 104 views
0

我必須對着名的「FizzBu​​zz」代碼做一個變化,計算出用戶輸入的數字,並且每行僅打印11個數字。我的代碼似乎有點工作,但它重複每行11次相同的東西。每行打印11個數字?

有人可以幫我解決這個問題嗎?

import javax.swing.JOptionPane; 

public class CozaLoza2 { 
    public static void main(String[] args) { 

     String n = JOptionPane.showInputDialog("Please input an interger"); 
     int result = Integer.parseInt(n); 
     for (int i = 0; i <= result; i++) { 
      for (int j = 0; j <= 10; j++) { 

       System.out.print(" "); 

       if (i % 3 == 0 && i % 5 == 0 && i % 7 == 0) { 
        System.out.print("CozaLozaWoza"); 
       } 

       if (i % 3 == 0 && i % 5 == 0) { 
        System.out.print("CozaLoza"); 

       } else if (i % 5 == 0) { 
        System.out.print("Loza"); 

       } else if (i % 3 == 0) { 
        System.out.print("Coza"); 

       } else if (i % 7 == 0) { 
        System.out.print("Woza"); 

       } else { 
        System.out.print(" " + i); 
       } 
      } 
     } 
     System.out.println(); 
    } 
} 

這裏的輸出我得到的,如果我輸入 「20」 的數字成程序

CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza CozaLozaWozaCozaLoza 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 4 4 4 4 4 4 4 4 4 4 4 Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza 8 8 8 8 8 8 8 8 8 8 8 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza 11 11 11 11 11 11 11 11 11 11 11 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 13 13 13 13 13 13 13 13 13 13 13 Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza Coza Loza Coza Loza Coza Loza Coza Loza Coza Loza Coza Loza Coza Loza Coza Loza Coza Loza Coza Loza Coza Loza 16 16 16 16 16 16 16 16 16 16 16 17 17 17 17 17 17 17 17 17 17 17 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 19 19 19 19 19 19 19 19 19 19 19洛薩洛薩洛薩洛薩洛薩洛薩洛薩洛薩洛薩洛薩洛薩

+0

它不清楚你所期望的精品工程。請發佈輸入和期望輸出 – Rehman

+0

請正確縮進您的代碼,您可以使用IDE爲您完成此操作。否則很難讀取代碼。 – user1803551

+0

是的,它重複了11次,這就是'for(int j ...'循環所做的事情。如果你不想這樣做,循環中的代碼應該可能以某種方式依賴於'j'。 – Henry

回答

1

我很高興地宣佈,你自己缺乏縮進是你的失敗。這個爛攤子

} 
} 
    } 
    System.out.println(); 
} 
} 

應該是這個爛攤子:

} 
} 
    System.out.println(); 
    } 
} 
} 

現在去學習如何縮進和解決您的壓痕。

另外,在外環for (int i = 0; i <= result; i++)中,您正在運行result + 1值。可能你需要i < result

(以防萬一,這個問題被編輯過,我說的是原件。)

0

你的代碼產生完全相同:(!末)

input (20 in your test) : for (int i=0; i<=result; i++) 

x 11 : for (int j=0; j<=10; j++) 

而且只有一個CRLF =>放它之前

0

可能你必須移動System.out.println();上面的一個括號。此代碼應該是

import javax.swing.JOptionPane; 

public class CozaLoza2 { 
    public static void main(String[] args) { 

     String n = JOptionPane.showInputDialog("Please input an interger"); 
     int result = Integer.parseInt(n); 
     for (int i = 0; i <= result; i++) { 
      for (int j = 0; j <= 10; j++) { 

       System.out.print(" "); 

       if (i % 3 == 0 && i % 5 == 0 && i % 7 == 0) { 
        System.out.print("CozaLozaWoza"); 
       } 

       if (i % 3 == 0 && i % 5 == 0) { 
        System.out.print("CozaLoza"); 

       } else if (i % 5 == 0) { 
        System.out.print("Loza"); 

       } else if (i % 3 == 0) { 
        System.out.print("Coza"); 

       } else if (i % 7 == 0) { 
        System.out.print("Woza"); 

       } else { 
        System.out.print(" " + i); 
       } 
      } 
      System.out.println(); // moved line 
     } 
    } 
}