英文:
Create a Square of stars with an inputed number
问题
System.out.print("请输入一个数字:");
side = input.nextInt();
for (int i = 0; i < side; i++) {
System.out.print("* ");
}
System.out.println(); // 换行
for (int i = 0; i < side - 2; i++) {
System.out.print("* "); // 左边的星号
for (int j = 0; j < side - 2; j++) {
System.out.print(" "); // 中间的空格
}
System.out.println("* "); // 右边的星号并换行
}
for (int i = 0; i < side; i++) {
System.out.print("* ");
}
输入:
5
输出:
* * * * *
* *
* *
* *
* * * * *
你的代码存在问题,主要是在打印内部空心部分时没有正确的处理。上面给出的代码在输出上述所需的空心正方形方面是正确的。
英文:
I need a code to help me print a square of stars with an Integer input (in this case the number is 5)
the square must be empty on the inside.
for example:
Looking for this output
* * * * *
* *
* *
* *
* * * * *
what I get
* * * *
*
*
*
* * * *
I am missing my right side of the square.
MY code:
System.out.print("Please enter a number: ");
side = input.nextInt();
for (int i = 0; i < side - 1; i++) {
System.out.print("* ");
}
for (int i = 0; i < side; i++) {
System.out.println("*");
}
for (int i = 0; i < side; i++) {
System.out.print("* ");
}
}
input
5
output
* * * *
*
*
*
* * * *
答案1
得分: 1
你可以使用嵌套的 for 循环来实现这个。
for (int i = 0; i < side; i++) {
for (int j = 0; j < side; j++) {
if (i == 0 || i == side - 1 || j == 0 || j == side - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
如果是第一行/列或者最后一行/列,打印 *
;否则打印两个空格。
英文:
You can do this with a nested for loop.
for (int i = 0; i < side; i++) {
for (int j = 0; j < side; j++) {
if (i == 0 || i == side - 1 || j == 0 || j == side - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
Print a *
if it is either first row/column or last row/column; otherwise print two spaces.
答案2
得分: 1
使用一个时间复杂度为O(n)的单个For循环可以完成。
public static void main(String[] args) {
System.out.print("请输入一个数字:");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if (number >= 3) {
String endPattern = new String(new char[number]).replace("使用一个时间复杂度为O(n)的单个For循环可以完成。
public static void main(String[] args) {
System.out.print("请输入一个数字:");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if (number >= 3) {
String endPattern = new String(new char[number]).replace("\0", " *");
String midPattern = String.format("%-" + (number - 2) * 2 + "s %s", " *", " *");
for (int i = 1; i <= number; i++) {
if (i == 1 || i == number) {
System.out.print(endPattern);
} else {
System.out.print(midPattern);
}
System.out.println();
}
}
}
", " *");
String midPattern = String.format("%-" + (number - 2) * 2 + "s %s", " *", " *");
for (int i = 1; i <= number; i++) {
if (i == 1 || i == number) {
System.out.print(endPattern);
} else {
System.out.print(midPattern);
}
System.out.println();
}
}
}
输出(输入为3)
请输入一个数字:3
* * *
* *
* * *
输出(输入为7)
请输入一个数字:7
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
<details>
<summary>英文:</summary>
**It can be done using a single For Loop with a Complexity of O(n)**
public static void main(String[] args) {
System.out.print("Please Enter a Number ");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if(number >= 3) {
String endPattern = new String(new char[number]).replace("
<details>
<summary>英文:</summary>
**It can be done using a single For Loop with a Complexity of O(n)**
public static void main(String[] args) {
System.out.print("Please Enter a Number ");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if(number >= 3) {
String endPattern = new String(new char[number]).replace("\0", " *");
String midPattern = String.format("%-"+(number-2)*2+"s %s", " *"," *");
for(int i=1; i<= number; i++) {
if(i==1 || i==number) {
System.out.print(endPattern);
}
else {
System.out.print(midPattern);
}
System.out.println();
}
}
}
**Output (for input 3)**
Please Enter a Number 3
* * *
* *
* * *
**output (for input 7)**
Please Enter a Number 7
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
</details>
# 答案3
**得分**: 0
你需要在中间的for循环结束后再添加一个额外的星号。可以通过嵌套第二个空格的for循环,然后打印星号来实现。
```java
for (int i = 0; i < side; i++) {
System.out.print("*");
for (int j = 0; j < side; j++) {
System.out.print(" ");
}
System.out.println("*");
}
", " *");
String midPattern = String.format("%-"+(number-2)*2+"s %s", " *"," *");
for(int i=1; i<= number; i++) {
if(i==1 || i==number) {
System.out.print(endPattern);
}
else {
System.out.print(midPattern);
}
System.out.println();
}
}
}
**Output (for input 3)**
Please Enter a Number 3
* * *
* *
* * *
**output (for input 7)**
Please Enter a Number 7
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
</details>
# 答案3
**得分**: 0
你需要在中间的for循环结束后再添加一个额外的星号。可以通过嵌套第二个空格的for循环,然后打印星号来实现。
```java
for (int i = 0; i < side; i++) {
System.out.print("*");
for (int j = 0; j < side; j++) {
System.out.print(" ");
}
System.out.println("*");
}
英文:
You need to add an extra star at the end of the middle for loop. This can be done by nested a second for loop of spaces followed by printing the star.
for (int i = 0; i < side; i++) {
System.out.print("*");
for (int j = 0; j < side; j++) {
System.out.print(" ");
}
System.out.println("*");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论