每行打印11个数字?

时间:2015-12-13 08:00:25

标签: java for-loop

我必须对着名的“FizzBu​​zz”代码进行更改,其中它会计入用户输入的数字,并且每行只打印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 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 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 13 13 13 13 13 13 13 13 13 13 13 Woza Woza Woza Woza Woza Woza Woza Woza Woza Woza WozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza CozaLoza 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 Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza Loza

3 个答案:

答案 0 :(得分:1)

我很高兴地宣布你自己缺少缩进是你的垮台。这个烂摊子

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

应该是这个烂摊子:

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

现在去学习如何缩进并修复你的缩进。

此外,在外部循环for (int i = 0; i <= result; i++)中,您运行的是result + 1个值。可能你需要i < result

(以防问题被编辑,我正在谈论原文。)

答案 1 :(得分:0)

您的代码完全生成:

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

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

只有一个CRLF(最后!)=&gt;把它放在

之前

答案 2 :(得分: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
        }
    }
}