2016-03-01 144 views
0

我必須編寫一個程序,通過方法使用「*」打印出一個三角形。我必須設計它來要求用戶輸入一個數字來表示*在三角形底部的數量。然後通過將該數字傳遞給printUpTriangle()方法來打印出三角形。唯一我有一個想法的是實際的代碼,使三角形是:在java中打印出一個三角形

public class Triangle { 


public static void triangle(int levels) { 

if(levels == 0) 

return; 

triangle(levels - 1); 


for(int y = 0; y < levels; y++) { 

System.out.print("*"); 

} 

System.out.println(); 

} 

我必須寫兩個方法:一個返回包含S的N份的String,連續&另一級聯一個使用你的makeRow()方法。它應該打印一個直角三角形,其中三角形的底部由s的n個副本組成,並且三角形的頂點在右側具有s的單個副本(這兩種方法都有一個int &字符串作爲變量)。

回答

0
public static void main (String[] args) throws java.lang.Exception 
{ 
    makeTriangle(5); 
    // change the above line if you need to input the number (5) from the user 
} 

public static void makeTriangle(Integer base_size) 
{ 
    for(int x = 1; x < base_size + 1; x++) 
    { 
     System.out.print(makeRow(x, base_size)); 
     System.out.println(); 
    } 
} 

public static String makeRow(Integer row_num, Integer base) 
{ 
    String row = ""; 
    for(int x = base; x >= 0; x--) 
    { 
     if (x < row_num) { row += "*"; } 
     else row += " "; 
    } 
    return row; 
} 

這使得三角形的「三角形的頂點有s上的單個副本右側

所以它會打印出來 * ** *** **** *****

0

你不需要兩個方法,只需要一個toString方法。

public class Triangle 
{ 
    private int width; 

/** 
    This class describes triangle objects that can be displayed 
    as shapes like this: 
    * 
    ** 
    *** 
*/ 
public class Triangle 
{ 
    private int width; 

    /** 
     Constructs a triangle. 
     @param aWidth the number of * in the last row of the triangle. 
    */ 
    public Triangle(int aWidth) 
    { 
     width = aWidth; 
    } 

    /** 
     Computes a string representing the triangle. 
     @return a string consisting of * and newline characters 
    */ 
    public String toString() 
    { 
     String r = ""; 
     for (int i = 1; i <= width; i++) 
     { 
     // Make triangle row 
     for (int j = 1; j <= i; j++) 
      r = r + "*"; 
     r = r + "\n"; 
     } 
     return r; 
    } 
} 

而且Tester類:

import java.util.Scanner; 

/** 
    This program prints two triangles. 
*/ 
public class TriangleRunner 
{ 
    public static void main(String[] args) 
    { 
     System.out.println("Please enter number"); 
     Scanner sc = new Scanner(System.in); 
     int i = sc.nextInt(); 

     Triangle test = new Triangle(i); 
     System.out.println(test); 
    } 
} 
+0

這將工作,如果我不得不輸入一個數字來確定在三角形底部*的數量? 「 –

+0

」@param aWidth三角形最後一行中*的數量。「 - 這意味着你給的數字是三角形的基礎。 – Foxy

+0

@JohnWalker增加了掃描儀等級,所以在三角形基礎上可以很容易地玩*。 – Foxy

0
public class Triangle { 
    public static void main(String[] args) { 
     printTriangle(10); 
    } 

    public static String makeRow(int n) { 
     return String.format("%1$-" + n + "s", "*").replace(" ", "*"); 
    } 

    public static void printTriangle(int n) { 
     IntStream.rangeClosed(1, n).mapToObj(Triangle::makeRow).forEach(System.out::println); 
    } 
} 
0

EDIT意識到OP需要RIGHT三角形。我錯誤地做了一個等邊三角形。新的輸出如下所示。

如果你真的必須有另一種方法來打印三角:

import java.util.Scanner; 

public class ICanHasTriangle { 

static Scanner input; 
static String s = "*"; 

public static void main(String args[]) { 

    input = new Scanner(System.in); 
    System.out.print("How wide will your base be (Enter an integer): "); 
    int width = input.nextInt(); 

    boolean valid = false; 

    while (!valid) { 
     if (width <= 0) { 
      System.out.println("ERROR. You must enter a positive integer!"); 
      System.out.println(); 
      System.out.print("Try again: "); 
      valid = false; 
      width = input.nextInt(); 
     } else { 
      valid = true; 
      printUpTriangle(s, width); 
     } 
    } 
} 
public static String printUpTriangle(String s, int width) { 

    for (int i = 0; i <= width; i++) { 

     for (int j = 0; j < i + 1; j++) { 

      System.out.print(s); 
     } 
     System.out.println(); 
    } 
    return s; 
} 
} 

輸出

How wide will your base be (Enter an integer): 0 
ERROR. You must enter a positive integer! 

Try again: 8 
* 
** 
*** 
**** 
***** 
****** 
******* 
******** 
********* 
+0

我剛纔意識到我做了一個等邊三角形,而且你需要一個直角三角形。那天我跳過了幾何學。哈哈。回到繪圖板! – IRGeekSauce