簡體   English   中英

初學者java - 向后打印一個直角三角形

[英]beginner java - printing a right triangle backwards

上一次我在這里,我在計算機科學家庭作業中需要幫助的唯一問題是在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--;
         }
   }
}

那么他現在要求我們做反過來。 這是實際的問題:

“編寫一個程序,繪制以下形狀的100行直線三角形:第一行,打印100 ',第二行,99' '......最后一行,只有一行'*'。使用for循環對於這個問題。將程序命名為PrintTriangle.java“

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

我確定它很簡單,但我到目前為止所嘗試的一切都已經失敗或者一次只創造了1個空間。 任何建議或幫助將不勝感激! 先感謝您!

好的,先來看看這兩個問題。 你會如何聯系他們。

由於第二個問題與第一個問題相反,所以你在第一個代碼中首先做的是,你需要在下一個問題中做到最后。

所以,你的循環實際上應該向下工作,它在下面的代碼中結束。

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

所以,想一想你需要做些什么來使這個循環向后工作。

提示 : -

  • 從0增加到100是向前的
  • 從100減少到0是向后的

     **** *** ** * 

此外,在您的上述模式中,您會看到在實際打印characters之前需要先打印spaces所以,您也需要考慮這一點。

所以,在這里你必須實際打印兩個不同的字符: -

  • 幾個Spaces ,緊接着,
  • 很少*'s

這是模式: -

  • 我們的最大行是max (在你的情況下是100)
  • 行( i )有( ispaces數(行0有0個空格,行1有1個空格)
  • 然后它有( n - i )個stars數(第0行有100顆星,第1行有99顆星)

所以,你可以看到你實際上需要two循環。

分析我所說的內容,並提出一些代碼。 試試看。

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

我只想告訴你如何做到這一點。 理解模式。

就像在上一個問題上打印*一樣,您需要打印空格。 然后按相反的順序打印星星。

*
**
***

***
 **
  *

對於這些金字塔(我稱之為金字塔),首先要確保你的空間正確

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

現在您可以看到兩種模式,現在您可以對其進行編程。 這是假的...

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

以下代碼可以幫助您找到解決方案。

 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