我必須對着名的「FizzBuzz」代碼做一個變化,計算出用戶輸入的數字,並且每行僅打印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洛薩洛薩洛薩洛薩洛薩洛薩洛薩洛薩洛薩洛薩洛薩
它不清楚你所期望的精品工程。請發佈輸入和期望輸出 – Rehman
請正確縮進您的代碼,您可以使用IDE爲您完成此操作。否則很難讀取代碼。 – user1803551
是的,它重複了11次,這就是'for(int j ...'循環所做的事情。如果你不想這樣做,循環中的代碼應該可能以某種方式依賴於'j'。 – Henry