Java 编写一个程序,显示从 100 到 1000 的所有数字,每行十个,可以被 5 和 6 整除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21808315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:46:55  来源:igfitidea点击:

Write a program that displays all the numbers from 100 to 1000, ten per line, that are divisible by 5 and 6

java

提问by emm

Write a program that displays all the numbers from 100 to 1000, ten per line, that are divisible by 5 and 6.

编写一个程序,显示从 100 到 1000 的所有数字,每行 10 个,可以被 5 和 6 整除。

this is my program:

这是我的程序:

for (int i =100; i<= 1000; i++){

    if(i % 5==0 && i % 6==0)
        System.out.print(i +" ");

    if (i %10==0){
        System.out.println();

}

采纳答案by JD Davis

You can't really use modulo division here because you aren't keeping track of the number of times you have printed a number. Simply add a counter, and place a new line every time that counter is equal to 10.

您不能在这里真正使用模除法,因为您没有跟踪打印数字的次数。只需添加一个计数器,并在每次计数器等于 10 时放置一个新行。

For example:

例如:

    int counter=0;
    for(int i=100;i<=1000; i++) {
        if(i % 5==0 && i % 6==0) {
            System.out.print(i +" ");
            counter++;
        }
        if(counter==10) {
            System.out.println();
            counter=0;
        }

Corrected for cross system compatibility

更正跨系统兼容性

Version without counter (wont work for all sets of numbers)

没有计数器的版本(不适用于所有数字组)

    for(int i=100;i<=1000; i++) {
        if(i % 5==0 && i % 6==0) 
            System.out.print(i +" ");
        if(i!=100 && (i - 100) % 300 == 0)
            System.out.println();
    }