java 初学者java - 向后打印直角三角形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12875366/
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-10-31 10:36:27  来源:igfitidea点击:

beginner java - printing a right triangle backwards

javageometryspacing

提问by Evan Lemmons

last time i was on here, the only question i needed help with in my computer science homework involved making a right triangle on 100 lines, here was the code for that:

上次我在这里时,我在计算机科学作业中唯一需要帮助的问题是在 100 行上制作一个直角三角形,这是它的代码:

    public class PrintTriangle {
   public static void main(String[] args) {
      // Print a right triangle made up of *
      // starting at 100 and ending with 1
         int i = 100;
         while (i > 0) {
           for (int j = 0; j < i; j++)
           System.out.print("*");
           System.out.println();
         i--;
         }
   }
}

well now he's asked us to do the inverse. here's the actual question:

好吧,现在他要求我们做相反的事情。这是实际问题:

"Write a program that will draw a right triangle of 100 lines in the following shape: The first line, print 100 '', the second line, 99 ''... the last line, only one '*'. Using for loop for this problem. Name the program as PrintTriangle.java"

"编写一个程序,将绘制一个 100 行的直角三角形,其形状如下:第一行,打印 100 ' ',第二行,99 ''...最后一行,只有一个 '*'。使用 for 循环对于这个问题。将程序命名为 PrintTriangle.java”

 *****
  ****
   ***
    **
     *

i'm sure it's something simple but everything i've tried up to this point has flopped or only created 1 space at a time. any suggestions or help would be greatly appreciated! thank you in advance!

我确信这很简单,但到目前为止我尝试过的一切都失败了,或者一次只创建了 1 个空间。任何建议或帮助将不胜感激!先感谢您!

回答by Rohit Jain

OK, first take a look on both the problems. How would you relate them.

好,先看看这两个问题。你会如何将它们联系起来。

Since the second problem is the reverse of the first, so what you did first in your first code, you need to do that last in this next problem..

由于第二个问题与第一个问题相反,因此您在第一个代码中首先执行的操作,您需要在下一个问题中最后执行。

So, your loop should actually work backwards where it ended in the below code.

所以,你的循环实际上应该在它在下面的代码中结束的地方向后工作。

int i = 100;
for (int j = 0; j < i; j++)
           System.out.print("*");

So, think what you need to do to make this loop work backwards.

所以,想想你需要做什么才能使这个循环向后工作。

Hint: -

提示:-

  • Incrementing from 0 to 100 is forward
  • Decrementing from 100 to 0 is backwards

    ****  
     ***  
      **
       *  
    
  • 从 0 到 100 递增是向前的
  • 从 100 减到 0 是倒退

    ****  
     ***  
      **
       *  
    

Also, in your above pattern you see that you need to first print spacesbefore actually printing your charactersSo, that too you need to consider.

此外,在上面的模式中,您看到您需要spaces在实际打印之前先打印,characters所以您也需要考虑。

So, here you have to actually print two different characters: -

所以,在这里你必须实际打印两个不同的字符:-

  • Few Spaces, followed by,
  • Few *'s.
  • 很少Spaces,其次是
  • 很少*'s

Here's the pattern: -

这是模式: -

  • Let's the maximum row is max(100 in your case)
  • Row (i) has (i) number of spaces(Row 0 has 0 space, Row 1 has 1 space)
  • Then it has (n - i) number of stars(Row 0 has 100 stars, Row 1 has 99 stars)
  • 让我们的最大行是max(在您的情况下为100)
  • 第 ( i)行有 ( i) 个spaces(第 0 行有 0 个空格,第 1 行有 1 个空格)
  • 然后它有 ( n - i) 个stars(第 0 行有 100 颗星,第 1 行有 99 颗星)

So, you can see that you actually need twoloops here.

因此,您可以看到这里实际上需要two循环。

Analyze what all I said, and come up with some code. Try it out.

分析我所说的,并拿出一些代码。试试看。

回答by Hisham

public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < i; j++)
                System.out.print(" ");
            for (int j = i; j < 100; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

回答by Subir Kumar Sao

I am just giving you idea how you can do it. Understand the pattern.

我只是让你知道如何做到这一点。理解模式。

Just like printed * on previous question you need to print spaces. Then print stars on reverse order.

就像上一个问题上的打印 * 一样,您需要打印空格。然后以相反的顺序打印星星。

*
**
***

***
 **
  *

回答by Bharat Sinha

For these kind of pyramids (what i call them) first make sure you get the spaces right

对于这些类型的金字塔(我称之为)首先确保你的空间是正确的

* * * * *
- * * * *
- - * * *
- - - * * 
- - - - *

Now you can see two patterns and now you can program it. Here is psuedocode...

现在您可以看到两个模式,现在您可以对其进行编程。这是伪代码...

FOR I=1 to N
    FOR J = 1 to I-1
        PRINT " "
    ENDFOR
    FOR K = 1 to N-I+1
        PRINT "*"
    ENDFOR
PRINT "\n"
ENDFOR

回答by Yasin Okumu?

 int i = 1;
 while (i =< 100) {
   // first put spaces as long as it is necessary
   // it will be i times less than 100
   // for example for the first line (i = 1), 99 space (100-i) and 1 star (i)
   // for the 50. line (i == 50), 50 space (100-i) and 50 stars (i)
   for(int j = 0; j < 100-i; j++)
     System.out.print(" ");

   // then the stars
   for (int j = 0; j < i; j++)
     System.out.print("*");
   System.out.println();
   i++;
 }

回答by Tushar Paliwal

The code below can help you to find solution.

下面的代码可以帮助您找到解决方案。

 class ReverseTriangle {
 public static void main(String[] args) {
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < i; j++)
            System.out.print(" ");
        for (int j = i; j < 100; j++)
            System.out.print("*");
        System.out.println();
    }
 }
 }

回答by Dipali More

public class PrintTriangle {
   public static void main(String[] args) {

       for(int i=10;i>0;i--)
          {
           for (int j = 1; j < i; j++)
           System.out.print("*");
           System.out.println();           
         }
   }
}

回答by Sandip Adisare

public static void printPyramid(int ln){
    for(int j=ln;j>0;j--){
        for(int i=ln;i>0;i--) {
            if (j >= i) {
                System.out.print("*");
            } else {
            }
            System.out.print(" ");
        }
        System.out.println();
    }
}

public static void printReversePyramid(int ln){
    for(int j=0;j<ln;j++){
        for(int i=ln;i>=0;i--) {
            if (j >= i) {
                System.out.print("*");
            } else {
            }
            System.out.print(" ");
        }
        System.out.println();
    }
}

public static void printDiamond(int ln){
    for(int j=0;j<2*ln+1;j++){
        for(int i=ln;i>=0;i--) {
            if (j >= i && j < ln+1) {
                System.out.print("*");
            } else if(j > ln && (2*ln)-j>=i){
                System.out.print("*");
            }
            else {
            }
            System.out.print(" ");
        }
        System.out.println();
    }
}