我必须对著名的“ FizzBuzz”代码进行修改,在该代码中,用户输入的数字最多,每行仅打印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 4 4 4 4 4 4 4 4 4 4 4 Loza 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 Woza Woza Woza Woza 8 8 8 8 8 8 8 8 8 8 8 8 8科萨Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza 1113 11 11 11 11 11 11 11 11 11 11 11 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 Woza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza 16 16 1 6 16 16 16 16 16 16 16 16 17 17 17 17 17 17 17 17 17 17 17 17 Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza Coza 1919 19 19 19 19 19 19 19 19 19 19 Loza Loza Loza Loza Loza Loza Loza Loza萝莎萝莎
最佳答案
我很高兴地宣布,您自己的缩进是您的失败。这个烂摊子
}
}
}
System.out.println();
}
}
应该是这个烂摊子:
}
}
System.out.println();
}
}
}
现在去学习如何缩进和修复缩进。
同样,在外循环
for (int i = 0; i <= result; i++)
中,您正在运行result + 1
值。可能您需要i < result
。(以防万一问题被编辑,我在说的是原文。)