Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.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/7/arduino/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,我是java新手,正在学习入门课程 我已经能够解决我的大部分问题,但我被困在最后一步 使用四个或更少的system.out.print或system.out.println语句,最终结果应该是这样的: ******* * ***** * **** * *** * ** * * ******* 我创造了这个 ******* * ***** * **** * *** * ** * * * 这是我的密码。你们有什么可以帮忙的吗 public class St

我是java新手,正在学习入门课程

我已经能够解决我的大部分问题,但我被困在最后一步

使用四个或更少的system.out.print或system.out.println语句,最终结果应该是这样的:

*******
* *****
*  ****
*   ***
*    **
*     *
*******
我创造了这个

*******
* *****
*  ****
*   ***
*    **
*     *
*
这是我的密码。你们有什么可以帮忙的吗

public class StarPatterns {
    public static void main(String[] args) {
        int col;
        int row;

        for (row = 6; row >= 0 ; row--) {
            if (row >= 0)
                System.out.print("*");

            for (col = row; col < 6; col++) {
                System.out.print(" ");
            }

            for (col = row; col > 0; col--) {
                System.out.print("*");
            }

            System.out.println();
        }
    }
}
公共类StarPatterns{
公共静态void main(字符串[]args){
int col;
int行;
对于(行=6;行>=0;行--){
如果(行>=0)
系统输出打印(“*”);
用于(列=行;列<6;列++){
系统输出打印(“”);
}
对于(列=行;列>0;列--){
系统输出打印(“*”);
}
System.out.println();
}
}
}
公共类星号{
公共静态void main(字符串[]args){
int行=7;
int col=7;
整数计数;

对于(int i=0;iEDIT:缺少一个:已修复

你怎么看:

public static void main(String[] args) {
    for (int row=0; row<7; row++) {
        for (int col=0; col<7; col++) {
            System.out.print((col == 0) || (row == 6) || (col > row) ? "*" : " ");
        }
        System.out.println();
    }
}

你可以在1中不使用三元运算符,因为我假设你还没有学会(或者至少你的导师不喜欢你使用它们):


确切的要求是什么?你可以用一个只包含整个输出的println来完成。他们需要循环来创建图像。非常感谢。我从来没有想过使用count变量来完成这项工作。没问题,我用4,3和2 System.out.print.编辑了我的上一个解决方案。从技术上讲,这不是正确的输出,因为它应该不是o应该是7行而不是6行,除非你有意稍微改变它,但它确实让人理解了正确的想法。
public static void main(String[] args) {

        int row = 7;
        int col = 7;
        int count;

        for (int i=0;i<row;i++)
        {
            count = i;
            //System.out.print("*");
            for (int c=0;c<col;c++)
            {   
                if (count == 0 || count == row-1 || c == 0)
                {
                    System.out.print("*");
                }
                else
                {
                    System.out.print(" ");
                    count--;
                }
            }
            System.out.println("");
        }

    }
public static void main(String[] args) {

        int row = 7;
        int col = 7;
        int count;
        String strNewLine = "*" + System.lineSeparator();
        String str = "*";
        String strToPrint;

        for (int i=0;i<row;i++)
        {
            count = i;
            for (int c=0;c<col;c++)
            {   
                if (count == 0 || count == row-1 || c == 0)
                {
                    if (c == row-1)
                    {
                        strToPrint = strNewLine;
                    }
                    else
                    {
                        strToPrint = str;
                    }

                    System.out.print(strToPrint);
                }
                else
                {
                    System.out.print(" ");
                    count--;
                }
            }
        }

    }
public static void main(String[] args) {
    for (int row=0; row<7; row++) {
        for (int col=0; col<7; col++) {
            System.out.print((col == 0) || (row == 6) || (col > row) ? "*" : " ");
        }
        System.out.println();
    }
}
*******
* *****
*  ****
*   ***
*    **
*     *
*******
public class StarPatterns {
    public static void main(String[] args) {
        int col;
        int row;

        for (row = 0; row < 7; row++) {
            for (col = 0; col < 7; col++) {
                String symbol = " ";
                if (row == 0 || col == 0 || col > row || row == 6) {
                    symbol = "*";
                }

                if (col == 6) {
                    symbol += System.getProperty("line.separator");
                }

                System.out.print(symbol);
            }
        }
    }
}
*******
* *****
*  ****
*   ***
*    **
*     *
*******