伙计们,我正在尝试在等边三角形的输入行中打印输入单词,并且该单词应该居中......这是我的代码
public void draw (){
int starTosStart = 0;
for (int rows=1; rows <= getHeight(); rows++)
{
String charToPrint = "*";
String wordToPrint = "";
if(rows == getRowNum()){
starTosStart = ( getRowNum() - getTextLabel().length() )/2;
for(int i=0;i<getTextLabel().length();i++){
wordToPrint += " "+getTextLabel().charAt(i);
}
}
for (int spaces=1; spaces <= number_of_stars; spaces++)
{
System.out.print(" ");
}
for (int star=1; star <= rows; star++)
{
if(rows == getRowNum()){
System.out.print(wordToPrint);
System.out.print(" ");
break;
}
System.out.print(charToPrint);
System.out.print(" ");
}
System.out.println("");
number_of_stars = number_of_stars - 1;
}
}
}
我的输出是
*
* *
* * *
* * * *
* * * * *
* * * * * *
Z E L D A
而所需的输出是
*
* *
* * *
* * * *
* * * * *
* * * * * *
* Z E L D A *
请问有什么帮助吗?谢谢你
最佳答案
你已经得到了它,只需更改此行:
System.out.print(wordToPrint);
System.out.print(" ");
按此
System.out.print("* " + wordToPrint + " *");
关于java - 在java中使一个单词在一行中居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30217867/