Java三角形以星号开头,以起始编号开头

时间:2019-03-29 06:45:27

标签: java

public class Main                                       
{                                       
  public static void main(String[] args)                                        
  {                                     
    triangle.shape(9);                                                                      
    triangle.shape(12);                                                                         
  }                                     
}

public class triangle                                   
{                                       
  public static void shape(int v)                                       
  {                                     
    for(int i=0; i < v; i++)                                        
    {                                       
      for(int a= v-1; a > i; a--)                                       
        System.out.print(" ");                                      
      for(int a= i*2;a >= 0;a--)                                        
        System.out.print("*");                                      
      System.out.println();                                     

      if(v <= 0)                                        
      {                                     
        System.out.println("Error");                                        
      }                                     
    }                                       
  }                                     
}

我是一个基础学习者,我正在尝试实现一个三角形,如果选择9,它将从9个星开始,例如9,7,5,3,1,但是如果输入了偶数,它将下降1,所以10将变成9,从而允许9,7,5,3,1模式,任何等于或小于1的数字错误。因此,我尝试在资源的帮助下实现类似的情况,但是我的输出是行的长度,为了达到目标我该怎么做?

4 个答案:

答案 0 :(得分:0)

尝试一下

    public static void shape(int v) {
    if (v <= 0) {
        System.out.println("Error");
    } else {
        for (int i = v; i > 0; i--) {
            for (int a = i; a < v; a++)
                System.out.print(" ");
            for (int a = i; a > 0; a--)
                System.out.print("* ");
            System.out.println();
        }
    }
}

我修改了形状方法以打印向下的三角形,但是您的代码无法处理偶数输入。

答案 1 :(得分:0)

我已经修改了您的代码。有关说明,请参见代码注释。

package main;

public class Main {

    public static void main(String[] args) {
        shape(9);
        shapeReverse(9);
        shape(12);
        shapeReverse(12);
    }

    public static void shape(int v) {

        // check if v is less than or equal to 1
        if (v <= 1) {
            System.out.println("Error");
            return;
        }

        // check if v is even or odd
        // if even then decrement by 1
        // if odd then do nothing
        v = v % 2 == 0 ? v - 1 : v;

        // declare and initialize a counter for spaces
        int space = 0;

        // loop until v is less than or equal to 0
        while (v > 0) {

            // print out the spaces
            for (int i = 0; i < space; i++) {
                System.out.print(" ");
            }

            // print out the *
            for (int i = 0; i < v; i++) {
                System.out.print("*");
            }

            // decrement v by 2 since each level of the triangle has a difference of 2
            // asterisk
            v = v - 2;

            // increment space by 1 since each level of the triangle starts at different
            // positions
            space++;

            // print a new line
            System.out.println();
        }
    }

    public static void shapeReverse(int v) {

        // check if v is less than or equal to 1
        if (v <= 1) {
            System.out.println("Error");
            return;
        }

        int tmp = 1;

        // declare and initialize a counter for spaces
        int space = v % 2 == 0 ? (v / 2) - 1 : (v / 2);

        // loop until tmp is less than or equal to v
        while (tmp <= v) {

            // print out the spaces
            for (int i = 0; i < space; i++) {
                System.out.print(" ");
            }

            // print out the *
            for (int i = 0; i < tmp; i++) {
                System.out.print("*");
            }

            // increment t by 2 since each level of the triangle has a difference of 2
            // asterisk
            tmp = tmp + 2;

            // decrement space by 1 since each level of the triangle starts at different
            // positions
            space--;

            // print a new line
            System.out.println();
        }
    }

}

样本输入:

shape(9);

示例输出:

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

样本输入:

shape(12);

示例输出:

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

样本输入:

shapeReverse(9);

示例输出:

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

样本输入:

shapeReverse(12);

示例输出:

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

答案 2 :(得分:0)

您现有的代码向我们提供了一个方法public static void shape(int v)。该方法让我们输入一个int值,该值确定要打印多少行。您想要有一种方法来输入一个int值来控制一行中的最大开始数。因此这两种方法之间有一个对应关系。然后,我提供了一个方法public static int setMaxStartsInRow(int starts)将一行中的最大起点转换为该形状的行。例如,我们要控制一行的最大起始数为9。结果是三角形应该有4行。我的方法int setMaxStartsInRow(int starts)输入9并得到4作为满足您要求的结果。

<?php
require_once "connect.php";
$sql = "SELECT highscore FROM users WHERE username = ?";
$stmt = $con->prepare($sql);
if ($stmt->bind_param("s", $_GET['q1']) === false) {
  die('binding parameters failed');
}
$stmt->execute() or die($con->error);
$stmt->bind_result($hs);
$stmt->fetch();
$stmt->close();
echo $hs;
?>

triangle.shape(triangle.setMaxStartsInRow(9));

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // triangle.shape(9);                                                                      
        // triangle.shape(12);  
        triangle.shape(triangle.setMaxStartsInRow(9));  
        // triangle.shape(triangle.setMaxStartsInRow(11));  
    }

}


public class triangle {


    public static void shape(int v) {
        for (int i = 0; i < v; i++) {
            for (int a = v - 1; a > i; a--)
                System.out.print(" ");
            for (int a = i * 2; a >= 0; a--)
                System.out.print("*");
            System.out.println();

            if (v <= 0) {
                System.out.println("Error");
            }
        }
    }

    public static int setMaxStartsInRow(int starts) {

        if (starts <= 1)
            throw new RuntimeException("Input Max Number of starts should be greater than 1");

        int validateStarts = 0;

        if (starts % 2 == 0) {
            validateStarts = starts - 1;
        } else {
            validateStarts = starts;
        }

        return validateStarts / 2 + 1;
    }
}

triangle.shape(triangle.setMaxStartsInRow(10));

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

triangle.shape(triangle.setMaxStartsInRow(9));

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

triangle.shape(triangle.setMaxStartsInRow(1));

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

答案 3 :(得分:0)

Java代码:

public class Test {
public static void main(String[] args){
        shape(5);
        shape(12);
    }
    public static void shape(int v)
    {
        if(v%2 == 0){
            v=--v;
        }
        int count = v;
        int round = v;
        if (v > 0 ) {
        for(int i=v; i >0; i=i-2) {
            for (int a = 0 ; a<=(round-count) ;a++)
                System.out.print(" ");
            for (int a = i ; a>0;a--)
                System.out.print("*");
            System.out.println();
            count = --count;
        }
    }
        else
            System.out.println("Error");
    }
}