简体   繁体   English

Java金字塔/三角形使用for循环

[英]java pyramid/triangle using for loop

i want to print a triangle/pyramid style like: 我想打印一个三角形/金字塔样式,如:

   1
  323
 54345
7654567

here is my code: 这是我的代码:

int lines = 5;

    for (int i = 1; i < lines; i++) {

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

        for (int j = i; j > 1; j--) { //this for loop is my problem. any solution?
            System.out.print(j);
        }

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

what i got is 我得到的是

   1
  223
 32345
4324567

i been studying codes while working at office and i think week long i still could not find a solution to this even i use search in Google. 我在办公室工作时一直在学习代码,我认为整整一周我仍然找不到解决方案,即使我在Google中使用搜索也是如此。 i am only into enhancing my logic through conditionals and no heavy object oriented or recursion yet. 我只是想通过条件来增强我的逻辑,并且还没有面向对象或递归的方法。

The problem in your first loop is a problem you figured out in your second one! 第一个循环中的问题是第二个循环中发现的问题! (and it has something to do with the largest number in the loop) (这与循环中的最大数目有关)

for (int j = i; j > 1; j--) { //this for loop is my problem. any solution?
    System.out.print(j);
}

Look at the numbers on the left of the pyramid. 查看金字塔左侧的数字。 They start where the ones on the right end (every line of the pyramid is symmetrical). 它们从右端的那些位置开始(金字塔的每一条线都是对称的)。 And the general formula for that number is i + i - 1 , where i is the line number from your outer loop. 该数字的一般公式为i + i - 1 ,其中i是您外循环中的行号。

The second row starts at 2 * i - 1 = 2 * 2 - 1 = 3 . 第二行从2 * i - 1 = 2 * 2 - 1 = 3 The third row starts at 2 * 3 - 1 = 5 etc. 第三行从2 * 3 - 1 = 5等开始。

Your second inner loop should therefore look like this: 因此,您的第二个内部循环应如下所示:

for (int j = i + i - 1; j > i; j--) {
    System.out.print(j);
}

Here is the complete fixed source. 这是完整的固定源。

You have to start at the i-th odd number. 您必须从第i个奇数开始。 This is i*2-1 . 这是i*2-1 And you stop at i . 而你在i停下来。 This also fixes a spacing difference introduced by changing it to lines = 4 . 这也固定了将其更改为lines = 4引入的间距差异。

int lines = 4;

for (int i = 1; i <= lines; i++) {

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

    for (int j = i*2-1; j > i; j--) { //this for loop is my problem. any solution?
        System.out.print(j);
    }

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

Run it here: http://ideone.com/AKsc1f 在这里运行: http : //ideone.com/AKsc1f

int lines = 4; 整数行= 4;

for (int i = 1; i <= lines; i++) { for(int i = 1; i <= lines; i ++){

for (int j = 1; j < lines-i+1; j++) { 
    System.out.print(" ");
}
//replace this
for(int j=0; j<i-1; j++) System.out.print(i*2-j-1);
 System.out.print(i);
 for(int j=; j<i-1;j++) System.out.print(i+j+1);
//==========
System.out.println();

} }

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

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