Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.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_Methods - Fatal编程技术网

Java 输出图形的方法

Java 输出图形的方法,java,methods,Java,Methods,我正在尝试编写一个输出下图的方法: * ** *** **** ***** 我尝试了所有方法,但我的代码总是输出以下内容: ***** **** *** ** * 这是我的密码: public void outputFigure(int y) { count1 = y; count2 = y; int spaces = 0; int x = y; int x2 = y; boolean s = false; whil

我正在尝试编写一个输出下图的方法:

    *
   **
  ***
 ****
*****
我尝试了所有方法,但我的代码总是输出以下内容:

*****
****
***
**
*
这是我的密码:

public void outputFigure(int y) {
    count1 = y;
    count2 = y;
    int spaces = 0;
    int x = y;
    int x2 = y;
    boolean s = false;
    while (s != true) {
        for (int i = spaces; i > 0; i--) {
            System.out.print(" ");
        }
        spaces++;
        if (spaces == y - 1) {
            s = true;
        }

        for (count2 = 0; count2 < x; count2++) {
            for (count1 = 0; count1 < x2; count1++) {
                System.out.print("*");
            }
            x2--;

            System.out.println();
        }
    }
}
public void outputFigure(整数y){
count1=y;
count2=y;
int空间=0;
int x=y;
int x2=y;
布尔s=假;
while(s!=真){
对于(int i=空格;i>0;i--){
系统输出打印(“”);
}
空格++;
if(空格==y-1){
s=真;
}
对于(count2=0;count2

任何帮助都将不胜感激。

循环太多;您需要一个从
0
y
的外部循环
i
(行的#)。然后从
i
y-1
的循环用于空格,另一个从
y-i-1
y
的循环用于星星。然后是一条新的线路。而且,这里不需要实例;因此,我们可以使方法
静态
。像

public static void outputFigure(int y) {
    for (int i = 0; i < y; i++) {
        for (int j = i; j < y - 1; j++) {
            System.out.print(" ");
        }
        for (int j = (y - i - 1); j < y; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
}
然后我们可以用它来生成数字,比如

public static void outputFigure(int y) {
    IntStream.range(0, y).forEachOrdered(i -> System.out.printf(
                    "%s%s%n", repeat(" ", y - i - 1), repeat("*", i + 1)));
}

太多的循环;您需要一个从
0
y
的外部循环
i
(行的#)。然后从
i
y-1
的循环用于空格,另一个从
y-i-1
y
的循环用于星星。然后是一条新的线路。而且,这里不需要实例;因此,我们可以使方法
静态
。像

public static void outputFigure(int y) {
    for (int i = 0; i < y; i++) {
        for (int j = i; j < y - 1; j++) {
            System.out.print(" ");
        }
        for (int j = (y - i - 1); j < y; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
}
然后我们可以用它来生成数字,比如

public static void outputFigure(int y) {
    IntStream.range(0, y).forEachOrdered(i -> System.out.printf(
                    "%s%s%n", repeat(" ", y - i - 1), repeat("*", i + 1)));
}

除了@Elliott Frisch所指出的,还有另一个版本:

       for (int i = 1; i <= 5; i++)
        {
            for (int j = 5; j >= 1; j--)
            {
                if (j <= i)
                {
                    System.out.print("*");
                }
                else
                {
                    System.out.print(" ");
                }
            }

            System.out.println();
        }
可以简化为

while (!s)
这是更“干净”和更简洁


<强>编辑:< /强>注释中指出的,您也可以考虑使用一个更有意义的变量名而不是<代码> s>代码>这样的名称对于调试可能非常混乱(或者如果您或其他人稍后需要修改代码)。

< P>除了Elliott Frisch所指出的,这里还有另一个版本:

       for (int i = 1; i <= 5; i++)
        {
            for (int j = 5; j >= 1; j--)
            {
                if (j <= i)
                {
                    System.out.print("*");
                }
                else
                {
                    System.out.print(" ");
                }
            }

            System.out.println();
        }
可以简化为

while (!s)
这是更“干净”和更简洁


<强>编辑:< /强>注释中指出的,您可能还想考虑使用一个更有意义的变量名而不是<代码> s>代码>这样的名称对于调试可能非常混乱(或者如果您或其他人稍后需要修改代码)。

即使使用有意义的变量名也更干净。代码> S/代码>实际上并没有多大意义。@ JuangasLuMeNeDuaTror——我编辑了一下,提到OP可能想考虑一个更有意义的名字。即使使用有意义的变量名也更干净。代码> S/<代码>实际上并没有多大意义。JuangCARSLeMeNeDuaTrA-是我编辑的,提到OP可能想考虑一个更有意义的名字。如果其中一个答案解决了这个问题,你能成为正确的解决方案吗?如果其中一个答案解决了这个问题,你能作为正确的解决方案吗?