简体   繁体   English

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

[英]beginner java - printing a right triangle backwards

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. 我确定它很简单,但我到目前为止所尝试的一切都已经失败或者一次只创造了1个空间。 any suggestions or help would be greatly appreciated! 任何建议或帮助将不胜感激! thank you in advance! 先感谢您!

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 从0增加到100是向前的
  • Decrementing from 100 to 0 is backwards 从100减少到0是向后的

     **** *** ** * 

Also, in your above pattern you see that you need to first print spaces before actually printing your characters So, that too you need to consider. 此外,在您的上述模式中,您会看到在实际打印characters之前需要先打印spaces所以,您也需要考虑这一点。

So, here you have to actually print two different characters: - 所以,在这里你必须实际打印两个不同的字符: -

  • Few Spaces , followed by, 几个Spaces ,紧接着,
  • Few *'s . 很少*'s

Here's the pattern: - 这是模式: -

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

So, you can see that you actually need two loops here. 所以,你可以看到你实际上需要two循环。

Analyze what all I said, and come up with some code. 分析我所说的内容,并提出一些代码。 Try it out. 试试看。

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();
        }
    }
}

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. 然后按相反的顺序打印星星。

*
**
***

***
 **
  *

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
 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++;
 }

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();
    }
 }
 }
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();           
         }
   }
}
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();
    }
}

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

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