嘿,大家好,我有一个简单的问题,我正在思考。我需要做的就是打印出消息和星号之间的距离:
应该是这样的:
这是我的代码:
if(msg.length() < 54)
{
for (int x = 0; x <= 60; x++)
{
System.out.print("*");
}
System.out.print("\n***");
int length=msg.length();
int whiteSpace = (54 - (msg.length())/2);
System.out.print(msg);
for (int x = 0; x <= 60; x++)
{
System.out.print("");
}
System.out.print("***");
for (int x = 0; x<=whiteSpace; x++)
{
System.out.print(" ");
}
System.out.print("\n");
for (int x = 0; x <= 60; x++)
{
System.out.print("*");
}
System.out.println("");
}
如果你们能帮助我,那就太好了。
最佳答案
如果你不设置边框的话,边框也会有问题,你知道如果message.length连星星都不会好看。 试试这个
public static void main(String[] args) {
String msg = "Hello Bebeb";
int border=60;
if(msg.length()%2 ==0){
border+=1;
}
if (msg.length() < 54) {
for (int x = 0; x <= border; x++) {
System.out.print("*");
}
System.out.print("\n***");
int length = msg.length();
int whiteSpace = ((54 - (msg.length())) / 2);
for (int x = 0; x <= whiteSpace; x++) {
System.out.print(" ");
}
System.out.print(msg);
for (int x = 0; x <= whiteSpace; x++) {
System.out.print(" ");
}
System.out.print("***");
for (int x = 0; x <= whiteSpace; x++) {
System.out.print(" ");
}
System.out.print("\n");
for (int x = 0; x <= border; x++) {
System.out.print("*");
}
System.out.println("");
}
}
关于Java - 消息之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47027569/