用输入的数字创建一个星号组成的正方形。

go评论60阅读模式

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(&quot;Please enter a number: &quot;);
side = input.nextInt();
for (int i = 0; i &lt; side - 1; i++) {
System.out.print(&quot;* &quot;);

}
for (int i = 0; i &lt; side; i++) {
System.out.println(&quot;*&quot;);
}

for (int i = 0; i &lt; side; i++) {
System.out.print(&quot;* &quot;);

}

}
``````

input

5

output

``````* * * *
*
*
*
* * * *
``````

答案1

``````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 &lt; side; i++) {
for (int j = 0; j &lt; side; j++) {
if (i == 0 || i == side - 1 || j == 0 || j == side - 1) {
System.out.print(&quot;* &quot;);
} else {
System.out.print(&quot;  &quot;);
}
}
System.out.println();
}
``````

Print a `* ` if it is either first row/column or last row/column; otherwise print two spaces.

答案2

``````使用一个时间复杂度为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
* * *
*   *
* * *
``````

``````请输入一个数字：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) {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if(number &gt;= 3) {
String endPattern = new String(new char[number]).replace(&quot;
<details>
<summary>英文:</summary>
**It can be done using a single For Loop with a Complexity of O(n)**
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
if(number &gt;= 3) {
String endPattern = new String(new char[number]).replace(&quot;\0&quot;, &quot; *&quot;);
String midPattern = String.format(&quot;%-&quot;+(number-2)*2+&quot;s  %s&quot;, &quot; *&quot;,&quot; *&quot;);
for(int i=1; i&lt;= number; i++) {
if(i==1 || i==number) {
System.out.print(endPattern);
}
else {
System.out.print(midPattern);
}
System.out.println();
}
}
}
**Output (for input 3)**
* * *
*   *
* * *
**output (for input 7)**
* * * * * * *
*           *
*           *
*           *
*           *
*           *
* * * * * * *
</details>
# 答案3
**得分**: 0

```java
for (int i = 0; i < side; i++) {
System.out.print("*");
for (int j = 0; j < side; j++) {
System.out.print("  ");
}
System.out.println("*");
}
&quot;, &quot; *&quot;);
String midPattern = String.format(&quot;%-&quot;+(number-2)*2+&quot;s  %s&quot;, &quot; *&quot;,&quot; *&quot;);

for(int i=1; i&lt;= number; i++) {
if(i==1 || i==number) {
System.out.print(endPattern);
}
else {
System.out.print(midPattern);
}
System.out.println();
}
}
}

**Output (for input 3)**

* * *
*   *
* * *

**output (for input 7)**

* * * * * * *
*           *
*           *
*           *
*           *
*           *
* * * * * * *

</details>

# 答案3
**得分**: 0

```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 &lt; side; i++) {
System.out.print(&quot;*&quot;);
for (int j = 0; j &lt; side; j++) {
System.out.print(&quot;  &quot;);
}
System.out.println(&quot;*&quot;);
}
``````

• 本文由 发表于 2020年10月3日 02:33:05
• 转载请务必保留本文链接：https://go.coder-hub.com/64176635.html
• for-loop
• java
• loops
• printing
• square

go 65

go 93

go 67

go 69