public class Envelope{
public static int width = 80;
public static int height = 10;
public static int distance = 40;
public static void main(String[] args) {
for(int i =0; i < 1; i++) {
System.out.print("+");
for(int b = 2; b<=width;b++) {
System.out.print("-");
}
for(int d =1; d<=1;d++) {
System.out.println("+");
for(int e=1; e<height; e++) {
System.out.print("|");
}
}
}
for(int a=1; a<height; a++) {
System.out.println("|");
}
for(int c = 11; c < 12; c++) {
System.out.print("+");
for(int f = 0; f<width;f++) {
System.out.print("-");
}
for(int g=1;g<=1;g++) {
System.out.print("+");
}
}
}
}
我的当前输出太长,因此我只使用println()
来显示我的当前输出,而不是print()
。
+--------------------------------------------------------------------------------|||||||||+
所以我的问题是如何在加号后面加上符号“-”,但是该“-”必须向右移动80次。
I want to do this...as an example
+-----------------------------------------------------------------------+
| |
| |
| |
| |
| |
| |
+-----------------------------------------------------------------------+
答案 0 :(得分:2)
您尝试过
System.out.println("-".repeat(80));
答案 1 :(得分:1)
正如@WJS在他的回答中提到的,如果您使用的是 Java 11 ,它会很有用。在这里,我提到了使用 Java <= 11 的另一种方法。
System.out.println("+");
// For **Java <= 7**
System.out.println(String.format("%0" + 80 + "d", 0).replace("0", "-"));
// For **Java >= 8**
System.out.println(String.join("", Collections.nCopies(10, "|")));
System.out.println("+");
注意:您可以根据需要设置其格式。
更新后的答案
正如您提到的预期输出那样,我做到了。另外,您可以使用任何方法来完成此操作,但据您了解,我使用了一种简单的解决方案。
System.out.print("+");
System.out.print(String.format("%0" + 80 + "d", 0).replace("0", "-"));
System.out.print("+");
System.out.println();
for (int i = 1; i <= 5; i++) {
System.out.printf("%s", "|");
System.out.printf("%81s", "|");
System.out.println();
}
System.out.print("+");
System.out.print(String.format("%0" + 80 + "d", 0).replace("0", "-"));
System.out.print("+");
让我知道它是否对您有用。
答案 2 :(得分:0)
System.out.println("-".repeat(80));
for(int i =0; i < 1; i++) {
System.out.println("+");
for(int b =0; b<i;b++) {
System.out.println("-".repeat(80));
}
}
我希望该符号“-”与“ +”在一行上,并且在该“-”的符号中再次出现一个“ +” :),所以第一个问题是我接下来如何使用该“-”到“ +”