【问题标题】:How not to print the same number for every row, I want different number at every position如何不为每一行打印相同的数字,我希望每个位置都有不同的数字
【发布时间】:2023-02-05 13:37:51
【问题描述】:

我想打印从 100000 到 1000000 的数字,每边用两个空格分隔。我想每行打印 15 列/15 个唯一数字,但每一行都打印相同的数字。如何解决这个问题

class practicex{

public static void main(String[] args) {
    pract();
}

static void pract(){

    for (long i=100000; i<1000000; i++){

    for(long q=0; q<15; q++){
        System.out.print("  " + i + "  ");
    }
    System.out.println();
}
}

}

enter image description here

【问题讨论】:

  • for 循环内部(在方法 pract 中)变量 i 的值没有改变。你知道如何使用调试器吗?如果这样做,请尝试使用它来调试您的代码。

标签: java


【解决方案1】:

您使用了错误的循环。 i 没有变化。你想要做的是有while -&gt; for的组合:

static void pract(){
    int i = 100000;
    while(i<1000001){
        for(int q=0; q<15; q++){
            System.out.print("  " + i + "  ");
            i++;
        }
        System.out.println();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多