简体   繁体   English

在Java中打印数字三角形

[英]Print triangle of numbers in java

I've been having some trouble with a homework assignment for my Java class. 我在Java类的家庭作业中遇到了一些麻烦。 In it, we're supposed to take in an integer between 1 and 13 and display three different triangles consisting of numbers. 在其中,我们应该采用1到13之间的整数,并显示三个由数字组成的不同三角形。 For example, if I were to enter 5, the result would be: 例如,如果我输入5,结果将是:

Triangle 1 三角形1

     1
     2  3
     4  5  6
     7  8  9 10
    11 12 13 14 15

Triangle 2 三角2

    1
    2 6
    3 7 10
    4 8 11 13
    5 9 12 14 15

Triangle 3 三角3

               5
            4  9
         3  8 12
      2  7 11 14
    1 6 10 13 15

I've already got the first Triangle going fine, but my big concern is the second triangle. 我已经有了第一个三角形,但是我最关心的是第二个三角形。 I haven't attempted the third one yet. 我还没有尝试过第三次。 The other thing is that my Professor is picky about what method we use in creating the project. 另一件事是我的教授对我们在创建项目中使用的方法很挑剔。 In other words, we can only use what he has taught us. 换句话说,我们只能使用他教给我们的东西。 He told us to use the System.out.printf("%3d", n) statement to space out the characters and we have to create them within a separate class. 他告诉我们使用System.out.printf(“%3d”,n)语句来分隔字符,我们必须在单独的类中创建它们。

The code for the first triangle is as follows: 第一个三角形的代码如下:

    void triangle1(int n)
    {
        int k = 1;
        for (int i = 1; i <= n; i++)
        {
            for (int j = 0; j < 1; j++)
            {
                System.out.printf("%3d", n);
                k += 1;
            }

            System.out.println();
        }
    }

So, pretty much, I need to follow that standard to create the other two triangles, but I'm really stuck on the second one and I don't know where to start. 因此,差不多,我需要遵循该标准来创建其他两个三角形,但是我真的停留在第二个三角形上,而且我不知道从哪里开始。 Any help will be much appreciated! 任何帮助都感激不尽!

Here is the way I would approach it. 这是我要处理的方式。

Programs print one line at a time, you cannot print half a line then start to print another line. 程序一次只能打印一行,您不能先打印一半行,然后再开始打印另一行。

With that being said, you should recognize the pattern in the triangles. 话虽如此,您应该识别三角形中的图案。

1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

You have the first number n , then you see the next row starts with n + 1 . 您拥有第一个数字n ,然后看到下一行以n + 1开头。 The next number starts in the row is (n + 1) + t where t = 4 . 行中的下一个数字为(n + 1) + t ,其中t = 4 There is a pattern there. 那里有一个模式。

The third row follows the same pattern. 第三行遵循相同的模式。 The first number is (n + 1) then you can calculate the others by + (t - 1) 第一个数字是(n + 1)然后您可以用+ (t - 1)计算其他数字

This can be done with a for loop, like you did in the first time. 可以像第一次一样,通过for循环来完成。

For the last triangle you can use the same process, just change the signs and t would equal something different. 对于最后一个三角形,您可以使用相同的过程,只需更改符号即可,而t等于其他值。

Algorithm writing is all about identifying patterns. 算法编写全部与识别模式有关。

If you look closely, you'll see there's a repeating pattern between each number and the one that follows on a given line. 如果仔细观察,您会发现每个数字和给定行上的数字之间都有一个重复的模式。

3 7 10 =>       [3 & 7 differ by 4][7 & 10 differ by 3]
4 8 11 13 =>    [4 & 8 differ by 4][8 & 11 differ by 3][11 & 13 differ by 2]
5 9 12 14 15 => [...   differ by 4][...           by 3][...            by 2][... by 1]

You can use that information to make the second triangle. 您可以使用该信息制作第二个三角形。 I'll leave the rest to you. 剩下的我留给你。 I hope that helps! 希望对您有所帮助!

Seems you are a CS-Student, thus I will not present a finished solution. 看来您是CS学生,因此我不会提出完整的解决方案。 I'll give you some hints how I would solve this. 我会给你一些提示,我将如何解决这个问题。

This is what the print statement has to do: 这是print语句必须执行的操作:

for i=1 j=0 print 1 对于i = 1 j = 0打印1

for i=2 j=0 print 2 对于i = 2 j = 0打印2

for i=2 j=1 print 6 对于i = 2 j = 1打印6

for i=3 j=0 print 3 对于i = 3 j = 0打印3

for i=3 j=1 print 7 对于i = 3 j = 1打印7

for i=3 j=2 print 10 对于i = 3 j = 2打印10

Find a formula that calculates the correct output from i and j, its a simple linear combination. 找到一个可以从i和j计算正确输出的公式,它是一个简单的线性组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM