Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何制作内部有空格的等腰三角形_Java - Fatal编程技术网

Java 如何制作内部有空格的等腰三角形

Java 如何制作内部有空格的等腰三角形,java,Java,我需要做一个等腰三角形,里面有这样的空间 输入等腰三角形中等边的大小:6 * ** * * * * * * ****** 到目前为止,我得到的是一个没有空格的三角形 System.out.println("Enter the size of the equal sides in an isosceles triangle: "); Scanner num = new Scanner(System.in); int i,j; int a = num.next

我需要做一个等腰三角形,里面有这样的空间

输入等腰三角形中等边的大小:6
*
**
* *
*  *
*   *
******

到目前为止,我得到的是一个没有空格的三角形

    System.out.println("Enter the size of the equal sides in an isosceles triangle: ");
    Scanner num = new Scanner(System.in);
    int i,j;

    int a = num.nextInt();
    for(i=0; i < a ; i++) 
    {    
        for(j=0; j<=i; j++) 
        { 
            System.out.print("*"); 

        } 

        System.out.println(); 
    }
System.out.println(“输入等腰三角形中等边的大小:”;
扫描器编号=新扫描器(System.in);
int i,j;
int a=num.nextInt();
对于(i=0;i对于(j=0;j,这里有一个我喜欢的实现。它使用一个三元表达式来计算
I
j
的每个值,要打印的字符是什么。当下列其中一个为真时,我们选择打印
*

  • 它是给定行上的第一个或最后一个字符
  • 这是三角形的最后一行
否则,我们将打印一个空格。请看下面的代码示例

int a = 6;
for (int i=0; i < a ; i++) {
    for (int j=0; j<=i; j++) {
        char chr = (i == a-1 || j == 0 || j == i) ? '*' : ' ';
        System.out.print(chr); 
    } 

    System.out.println(); 
}

*
**
* *
*  *
*   *
******
inta=6;
for(int i=0;i对于(int j=0;jTrand)考虑您实际上要打印的不同代码< > >的值(i,j)
。在我看来,有两个特定的值
j==0
j==i
,您希望在其中打印一个星号,大多数情况下,您更希望打印一个空格。我试图在第二个循环中设置一个条件,但这似乎与我必须为行打印的空格有关。