无法从 Loops 打印“FlimFlam”

编写一个程序来执行以下任务。• 从命令行获取单个整数参数n。• 打印出从1 到n 的整数,每行一个,但对于3 的倍数,打印“Flim”而不是数字,对于5 的倍数,打印“Flam”而不是数字。对于 3 和 5 的倍数,打印“FlimFlam”。


例如,运行 java 示例 6 应生成输出 1 2 Flim 4 Flam Flim


class Example {

    public static void main (String argv[]) {

        if (argv.length != 1)

        usage();

        int n = 0;

        try {

        n = Integer.parseInt(argv[0]);

        } catch (NumberFormatException e) {

        usage();

        }

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

            if (i % 3 == 0) {

                System.out.println("Flim");

            } else if (i % 5 == 0) {

                System.out.println("Flam");

            } else if (i % 3 == 0 && i % 5 == 0){

                System.out.println("FlimFlam");

            } else {

                System.out.println(i);

        }

  }


  private static void usage() {

    System.err.println("usage: java Example count string");

    System.exit(1);

  }

}


FFIVE
浏览 83回答 2
2回答

largeQ

问题出在条件上。由于要么i % 3ori % 5首先满足,所以它永远不会达到i % 3 == 0 && i % 5 == 0条件。因此,您必须首先检查i % 3 == 0 && i % 5 == 0然后检查其余两个条件。以下是修改后的条件:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i % 3 == 0 && i % 5 == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("FlimFlam");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (i % 3 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Flim");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (i % 5 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Flam");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }编辑: - if-else-if 块就像如果任何第一个出现的条件匹配,则不要检查 if-else-if 块中的其余条件。

心有法竹

这是完整的代码:public class Example {public static void main(String argv[]) {&nbsp; &nbsp; if (argv.length != 1)&nbsp; &nbsp; &nbsp; &nbsp; usage();&nbsp; &nbsp; &nbsp; &nbsp; int n = 0;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; n = Integer.parseInt(argv[0]);&nbsp; &nbsp; &nbsp; &nbsp; } catch (NumberFormatException e) {&nbsp; &nbsp; &nbsp; &nbsp; usage();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= n; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i % 3 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i % 5 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("FlimFlam");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Flim");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (i % 5 == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Flam");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(i);&nbsp; &nbsp; &nbsp; &nbsp; }}private static void usage() {&nbsp; &nbsp; System.err.println("usage: java Example count string");&nbsp; &nbsp; System.exit(1);&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java