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

Java 倒立等腰三角形

Java 倒立等腰三角形,java,loops,Java,Loops,答复: 我的任务是编写一个Java程序,根据用户的输入输出等腰三角形。例如,如果用户在收到提示后输入数字5,程序将输出 ***** **** *** ** * 我们被指示使用while循环,但我在这方面没有任何成功。我决定改用for循环,但仍然有问题。我已经声明了我的变量,并提示用户输入。下面是我的for循环。请帮忙!我的程序所做的就是打印出一列连续的英镑符号 //For loop for (CountOfRows=UserInput; CountOfRows>0; Co

答复:

我的任务是编写一个Java程序,根据用户的输入输出等腰三角形。例如,如果用户在收到提示后输入数字5,程序将输出

*****
****
***
**
*
我们被指示使用while循环,但我在这方面没有任何成功。我决定改用for循环,但仍然有问题。我已经声明了我的变量,并提示用户输入。下面是我的for循环。请帮忙!我的程序所做的就是打印出一列连续的英镑符号

    //For loop
    for (CountOfRows=UserInput; CountOfRows>0; CountOfRows--)
    {
        System.out.println("# ");
        for (CountOfColumns=UserInput; CountOfColumns>0; CountOfRows++)
        {
            System.out.println("# ");
        }

    }

如果您想使用while循环,只需执行以下操作:

while(num > 0){ //stay in loop while the number is positive
    int temp = num; //make a copy of the variable
    while(temp-- > 0) //decrement temp each iteration and print a star
        System.out.print("*"); //note I use print, not println
    System.out.println(); //use println for newline
    num--; //decrement number
}

您需要更改内部for循环,使其从第一个for循环运行到索引结束,如下所示:

 int num = 5;

    for (int i = num; i > 0; i--) {
        for (int j = 0; j < i; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
int num=5;
对于(int i=num;i>0;i--){
对于(int j=0;j
println
打印您给它的字符串和换行符。