我打印了一個等邊三角形,並且在某一行的中間我想打印一個單詞(作爲輸入)。我的代碼如下:在Java中打印一個單詞
for (int rows=1; rows <= getHeight(); rows++)
{
char charToPrint = '*';
if(rows == getRowNum()){
for(int i=0;i<getTextLabel().length();i++){
charToPrint = getTextLabel().charAt(i);
}
}
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;
}
輸出:
*
* *
* * *
* * * *
* * * * *
* * * * * *
A A A A A A A
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
,但我想輸出是這樣的:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* L R E Z A *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
單詞的長度是否等於我們想要打印的rownumber? –
@akhil_mittal等於或小於它 –
輸入詞將有空格像'L R E Z A'或不像'LREZA'? –