首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每行打印11个数字?

每行打印11个数字?
EN

Stack Overflow用户
提问于 2015-12-13 08:00:24
回答 3查看 451关注 0票数 0

我必须对著名的"FizzBuzz“代码做一个变体,它计数到用户输入的数字,并且每一行只打印11个数字。我的代码似乎有点工作,但它重复相同的事情11次每一行。

有人能帮我把这个修好吗?

代码语言:javascript
复制
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 1 1 1 8 8 8CozaLoza 16 16 16 17 17 17 19 19 19

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-13 08:25:09

我很高兴地宣布,你自己没有缩痕是你的垮台。这乱七八糟

代码语言:javascript
复制
  }
}
  }
     System.out.println();
}
}

应该是这样的混乱:

代码语言:javascript
复制
  }
}
     System.out.println();
  }
}
}

现在去学习如何缩进和修复您的缩进。

此外,在外部循环for (int i = 0; i <= result; i++)中,您正在result + 1值上运行。也许你需要i < result

(万一这个问题被编辑了,我说的是原文。)

票数 1
EN

Stack Overflow用户

发布于 2015-12-13 08:25:27

您的代码准确地生成了:

代码语言:javascript
复制
input (20 in your test) : for (int i=0; i<=result; i++)

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

而且只有一个CRLF (在结尾!) =>把它放在前面。

票数 0
EN

Stack Overflow用户

发布于 2015-12-13 08:27:17

可能您必须将System.out.println();移到上面的一个括号。这段代码应该很好。

代码语言:javascript
复制
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
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34249097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档