-1

抱歉这个措辞不好的问题,今天早上我很着急,因为课程要结束了。

--

final int maxS = height;
for (int row = 1; row <= maxS; row ++)
{
    for (int star = 1; star <= maxS; star++)
        System.out.print("* ");
    System.out.println();
}

我需要这是输出:

* * * * * 
* * * * 
* * * 
* * 
* 

所以高度是行数,即 5。我可以得到这个

* 
* * 
* * * 
* * * * 
* * * * * 

使用与上面类似的代码,但它对我不起作用。我试图得到相反的结果,但没有奏效。

4

3 回答 3

2

尝试这个:

    final int maxS = 10;

    for (int row = 1; row <= maxS; row ++) {
        for (int star=maxS; star >= row; star--) {
            System.out.print("* ");
        }
        System.out.println();
    }
于 2013-10-01T13:26:25.047 回答
1

Try:

int maxS = height;
for (int row = 1; row <= maxS; row ++)
{
    for (int star = maxS - row + 1; star >= 1; star--)
    System.out.print("* ");
    System.out.println();
}

You have to think step by step. What happens at each outer loop, and each inner loop. It is very easy actually. If you cannot get it working, you should manually go through each loop on paper. You want it to decrease at each iteration, so you have to use -- instead of ++.

于 2013-10-01T13:24:06.437 回答
1

使用您的实际代码,您将得到一个正方形,这是一个有关如何实现它的示例:

我已将第一个替换为for

for (int row = maxS; row >= 1; row--)

第二个for是:

for (int star = 1; star <= row; star++)

 

public static void main(String[] arv) throws Exception {
    final int maxS = height;
    for (int row = maxS; row >= 1; row--) {
        for (int star = 1; star <= row; star++) {
            System.out.print("* ");
        }
        System.out.println();
    }
}
于 2013-10-01T13:18:42.623 回答