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

huangapple go评论70阅读模式
英文:

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

得分: 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 &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

得分: 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(&quot;Please Enter a Number &quot;);
		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) {
System.out.print(&quot;Please Enter a Number &quot;);
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)**
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("*");
}
&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)** 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 &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;);
}

huangapple
  • 本文由 发表于 2020年10月3日 02:33:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64176635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定