在java中打印出一个三角形

时间:2016-03-01 02:26:05

标签: java eclipse

我必须编写一个程序,使用" *"打印出一个三角形。通过方法。我必须设计它以询问用户一个数字来表示三角形底部的*的数量。然后通过将该数字传递给printUpTriangle()方法打印出三角形。我唯一想到的是制作三角形的实际代码:

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

}

我必须编写两个方法:一个返回一个包含n个s副本的字符串,连接在一起&amp;另一个使用你的makeRow()方法。它应该打印一个直角三角形,其中三角形的底边由n个副本组成,三角形的顶点在右边有一个s副本(两个方法都有一个int&amp; String作为变量)。 / p>

4 个答案:

答案 0 :(得分: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);
   }
}

答案 1 :(得分: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);
    }
}

答案 2 :(得分:0)

编辑意识到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
*
**
***
****
*****
******
*******
********
*********

答案 3 :(得分: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副本

所以它会打印出来 * ** *** **** *****