有没有办法打印出没有 'adding' 空格的文本?
Is there a way to print out text without 'adding' spaces?
我正在研究一种输出此内容的方法:
**********
* *
* *
* Hello *
* *
* *
**********
我觉得我需要使用 .substring 方法,但卡住了。许多消息来源告诉我使用嵌套循环,这就是我所做的,除了当我尝试将我的消息放入形状时,由于字符串将星号移动到右边:
***********
* *
* *
* Hello *
* *
* *
***********
我正在使用 java 8. int n 是放置在形状顶部和底部的 c 个字符的数量。在这种情况下,字符串消息将是你好。
public void boxThing(int n, char c, String message) {
for(int i = 0; i <= (n/2); i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || i == n) {
System.out.print(c);
}
else if(j == 0 || j == n) {
System.out.print(c);
}
else if(j==n/2 && i==((n/2)/2)+1){
System.out.print(message);
}
else{
System.out.printf(" ");
}
}
System.out.println();
}
for(int i =0; i<=n; i++){
System.out.print(c);
}
}
你要把消息的长度加上j减1,比如"hello"这个词占了5个位置。由于 j 将在循环中为单词添加 1,因此当它到达第二个 else if 条件时,您只需将 j 设置为 j + 消息长度 -1。参考下文。
for(int i = 0; i <= (n/2); i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || i == n) {
System.out.print(c);
}
else if (j == 0 || j == n) {
System.out.print(c);
}
else if (j == n/2 && i == ((n/2)/2)+1) {
System.out.print(message);
j = j + message.length() - 1;
}
else {
System.out.print(" ");
}
}
System.out.println();
}
for(int i = 0; i <= n; i++){
System.out.print(c);
}
你快到了..
当 "column" 包含您已打印的消息时,您无需打印任何内容。
即。 "Hello" 是从索引 3 的列开始的 5 个字符。
这意味着,索引 3-7 已经包含 "Hello",因此,您不应在此列之间打印任何字符。
我正在研究一种输出此内容的方法:
**********
* *
* *
* Hello *
* *
* *
**********
我觉得我需要使用 .substring 方法,但卡住了。许多消息来源告诉我使用嵌套循环,这就是我所做的,除了当我尝试将我的消息放入形状时,由于字符串将星号移动到右边:
***********
* *
* *
* Hello *
* *
* *
***********
我正在使用 java 8. int n 是放置在形状顶部和底部的 c 个字符的数量。在这种情况下,字符串消息将是你好。
public void boxThing(int n, char c, String message) {
for(int i = 0; i <= (n/2); i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || i == n) {
System.out.print(c);
}
else if(j == 0 || j == n) {
System.out.print(c);
}
else if(j==n/2 && i==((n/2)/2)+1){
System.out.print(message);
}
else{
System.out.printf(" ");
}
}
System.out.println();
}
for(int i =0; i<=n; i++){
System.out.print(c);
}
}
你要把消息的长度加上j减1,比如"hello"这个词占了5个位置。由于 j 将在循环中为单词添加 1,因此当它到达第二个 else if 条件时,您只需将 j 设置为 j + 消息长度 -1。参考下文。
for(int i = 0; i <= (n/2); i++) {
for (int j = 0; j <= n; j++) {
if (i == 0 || i == n) {
System.out.print(c);
}
else if (j == 0 || j == n) {
System.out.print(c);
}
else if (j == n/2 && i == ((n/2)/2)+1) {
System.out.print(message);
j = j + message.length() - 1;
}
else {
System.out.print(" ");
}
}
System.out.println();
}
for(int i = 0; i <= n; i++){
System.out.print(c);
}
你快到了..
当 "column" 包含您已打印的消息时,您无需打印任何内容。
即。 "Hello" 是从索引 3 的列开始的 5 个字符。 这意味着,索引 3-7 已经包含 "Hello",因此,您不应在此列之间打印任何字符。