Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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中使用for循环的等边三角形_Java_Console - Fatal编程技术网

Java中使用for循环的等边三角形

Java中使用for循环的等边三角形,java,console,Java,Console,我试图画一个由星号字符组成的等边三角形,但当用户输入行号时,该行将被画成x,整个三角形将是*但空格中有一个错误 这是我的密码: int number_of_stars = getHeight(); for (int rows=1; rows <= getHeight(); rows++) { for (int spaces=1; spaces <= number_of_stars; spaces++) { Sy

我试图画一个由星号字符组成的等边三角形,但当用户输入行号时,该行将被画成x,整个三角形将是*但空格中有一个错误

这是我的密码:

int number_of_stars = getHeight();
for (int rows=1; rows <= getHeight(); rows++) 
       {
        for (int spaces=1; spaces <= number_of_stars; spaces++) 
        {
            System.out.print(" ");
        }

        if(rows == getRowNum()){
            for (int star=1; star <= rows; star++) 
            {
                System.out.print("x");
                System.out.print(" ");
                }
            System.out.println("");
            rows = getRowNum()+1;
            System.out.print(" ");
            System.out.print(" ");
            System.out.print(" ");

        }
        for (int star=1; star <= rows; star++) 
        {
            System.out.print("*");
            System.out.print(" ");
            }
        System.out.println("");
        number_of_stars = number_of_stars - 1;
        }

第九行和第十行不正确

我相信您想要一个简单的if-else,当您添加一个额外的循环来打印xs时,它取消了对齐。我想你只是想

public static void main(String[] args) {
    int number_of_stars = getHeight();
    for (int rows = 1; rows <= getHeight(); rows++) {
        for (int spaces = 1; spaces <= number_of_stars; spaces++) {
            System.out.print(" "); // <-- indent(s)
        }
        for (int star = 1; star <= rows; star++) {
            if (rows == getRowNum()) {
                System.out.print("x"); // <-- one row is "x"
            } else {
                System.out.print("*"); // <-- others are "*"
            }
            System.out.print(" ");
        }
        System.out.println("");
        number_of_stars = number_of_stars - 1;
    }
}

对于特定的行号,您要做的唯一更改是打印x而不是*并且您可以简单地将字符更改为打印。我尝试了以下示例代码:

public static void main(String[] args) {
        int userRowNumber = 5;
        int height = 10;
        int number_of_stars = height;
        String charToPrint;
        for (int rows=1; rows <= height; rows++)
        {
            charToPrint = "*";
            if(rows == userRowNumber){
                charToPrint = "x";
            }
            for (int spaces=1; spaces <= number_of_stars; spaces++)
            {
                System.out.print(" ");
            }
            for (int star=1; star <= rows; star++)
            {
                System.out.print(charToPrint);
                System.out.print(" ");
            }
            System.out.println("");
            number_of_stars = number_of_stars - 1;
        }
    }

打印出预期的内容。对于代码中的问题,我建议您自己调试并找出答案。

现在是启动调试器并逐步执行的正确时机。
public static void main(String[] args) {
    int number_of_stars = getHeight();
    for (int rows = 1; rows <= getHeight(); rows++) {
        for (int spaces = 1; spaces <= number_of_stars; spaces++) {
            System.out.print(" "); // <-- indent(s)
        }
        for (int star = 1; star <= rows; star++) {
            if (rows == getRowNum()) {
                System.out.print("x"); // <-- one row is "x"
            } else {
                System.out.print("*"); // <-- others are "*"
            }
            System.out.print(" ");
        }
        System.out.println("");
        number_of_stars = number_of_stars - 1;
    }
}
public static void main(String[] args) {
        int userRowNumber = 5;
        int height = 10;
        int number_of_stars = height;
        String charToPrint;
        for (int rows=1; rows <= height; rows++)
        {
            charToPrint = "*";
            if(rows == userRowNumber){
                charToPrint = "x";
            }
            for (int spaces=1; spaces <= number_of_stars; spaces++)
            {
                System.out.print(" ");
            }
            for (int star=1; star <= rows; star++)
            {
                System.out.print(charToPrint);
                System.out.print(" ");
            }
            System.out.println("");
            number_of_stars = number_of_stars - 1;
        }
    }