以不同的数字开始,在Java中生成一个数字图案

发布于 2025-02-09 00:09:19 字数 595 浏览 1 评论 0原文

我正在准备编程考试。而且我陷入了这项任务,我了解模式的逻辑(,至少我认为),但我不知道如何解决这个问题。

输入的输出a = 6需要如下:

012345
123456
234567
345678
456789
567890

输入a = 3需要如下:

012
123
234

输入a = 4需要就像:

0123
1234
2345
3456

但是我明白了:

0 1 2 3 4 5 
1 2 3 4 5 
2 3 4 5 
3 4 5 
4 5 
5 

这是我的代码:

for (int i = 0; i <= a; i++) {

    for (int j = i; j < a; j++) {
        System.out.print(j + " ");
    }

    System.out.println();
}

I am preparing for my exam from programming. And I am stuck on this task, I understand logic of patterns ( at least I think so ) but I can't figure out how to solve this.

Output for input a = 6 needs to be like :

012345
123456
234567
345678
456789
567890

Output for input a = 3 needs to be like :

012
123
234

Output for input a = 4 needs to be like :

0123
1234
2345
3456

But I'm getting this :

0 1 2 3 4 5 
1 2 3 4 5 
2 3 4 5 
3 4 5 
4 5 
5 

Here's my code:

for (int i = 0; i <= a; i++) {

    for (int j = i; j < a; j++) {
        System.out.print(j + " ");
    }

    System.out.println();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2025-02-16 00:09:19

您需要进行这些更改以获取所需的输出:

  • 循环的外部的终止条件应为i&lt;;

  • 循环的内部的终止条件应为j&lt; a + i;


  • 您要打印的号码不应该是j,而是j%10满足您已提供的数据样本(对于输入a = 6 < /code>它打印0作为最后一行中的最后一个数字,而不是10,这意味着我们不需要j,而只需要最右数是10的其余部分。


这就是可以修复的方式:

public static void printNumbers(int a) {
    for (int i = 0; i < a; i++) {
        for (int j = i; j < a + i; j++) {
            System.out.print(j % 10 + " ");
        }
        System.out.println();
    }
}

You need to make these changes to get the required output:

  • Termination conditions of the outer for loop should be i < a;

  • Termination conditions of the inner for loop should be j < a + i;

  • The number you're printing should be not j but j % 10 to satisfy the data sample you've provided (for input a=6 it prints 0 as the last number on the very last line instead of 10, that means we need not j, but only its rightmost digit which is the remainder of division by 10).

That's how it can be fixed:

public static void printNumbers(int a) {
    for (int i = 0; i < a; i++) {
        for (int j = i; j < a + i; j++) {
            System.out.print(j % 10 + " ");
        }
        System.out.println();
    }
}
只是偏爱你 2025-02-16 00:09:19

观察:
由于它是2维输出,因此我们需要2个循环(一个内部循环)。

同样,每行的起点是最后一行启动+1的值。

如果值交叉10,则我们将仅保留单位数字值(如最后一行中)。 (因此,我们使用有助于提取单位数字的模量运算符%)

代码:

class Main {
    public static void main(String[] args) {
        int n = 6;
        pattern(n);
    }

    private static void pattern(int n) {
        int k = 0;
        for (int i = 0; i < n; i++) {
            for (int j = k; j < k + n; j++) {
                System.out.print(j % 10);
            }
            k = i + 1;
            System.out.println();
        }
    }

}

答案是:

012345
123456
234567
345678
456789
567890

Observations:
Since it is a 2 dimensional output, we will need 2 loops ( one inside the other ).

Also, the starting point for each line is the value from which the last line starts +1 .

If the value crosses 10, then we will keep the unit digit value only ( like in the last line). ( therefore we use modulus operator % that helps to extract the unit digit)

Code :

class Main {
    public static void main(String[] args) {
        int n = 6;
        pattern(n);
    }

    private static void pattern(int n) {
        int k = 0;
        for (int i = 0; i < n; i++) {
            for (int j = k; j < k + n; j++) {
                System.out.print(j % 10);
            }
            k = i + 1;
            System.out.println();
        }
    }

}

and the answer is :

012345
123456
234567
345678
456789
567890
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文