如何在java中仅使用while循环打印带星号的正方形?
我想知道如何在java中打印一个只有while循环的带星号的正方形 但是没有out for循环,没有任何逻辑操作,比如:和或 这段代码应该是这样的:如果我们输入4,它应该打印:如何在java中仅使用while循环打印带星号的正方形?,java,if-statement,while-loop,Java,If Statement,While Loop,我想知道如何在java中打印一个只有while循环的带星号的正方形 但是没有out for循环,没有任何逻辑操作,比如:和或 这段代码应该是这样的:如果我们输入4,它应该打印: * * * * * * * * * * * * 但它还做了其他一些事情,比如: * ** * 您可以注意到,必须仅在边框值上打印星号。也就是说,当x或y为0或x或y等于平方边-1时 for (int x=0; x < squareside; ++x) { for (int y=
* * * *
* *
* *
* * * *
但它还做了其他一些事情,比如:
* **
*
您可以注意到,必须仅在边框值上打印星号。也就是说,当x或y为0或x或y等于平方边-1时
for (int x=0; x < squareside; ++x) {
for (int y=0; y < squareside; ++y)
if (x == 0 || x+1 == squareside ||
y == 0 || y+1 == squareside) {
System.out.print("*");
} else {
System.out.print(" ");
}
System.out.println();
}
快速和冗长,但应该工作。除了while循环之外,它不使用if或其他任何东西。请随意优化:
public static void main(String... args) {
// logic :
// print first line * * * *
// print the rest of lines * * in a loop
// print last line * * * *
int len = 5; // user input
int x = 0;
// print first line
while (x++ < len) {
System.out.print("* ");
}
System.out.println(); // new line
// print the rest of lines * * in a loop
x = 0;
while (x++ < len - 2) {
System.out.print('*'); // beginning of line
int y = 0;
while (y++ < len - 2) {
System.out.print(" "); // dobule spaces
}
System.out.println(" *"); // end of line
}
x = 0;
// print the last line
while (x++ < len) {
System.out.print("* ");
}
}
这是解决这个问题的简单方法。如果需要,可以在星号之间轻松添加空格:
// Square.java
// Create a hollow square of asterisks.
// The side size is entered by the user
import java.util.Scanner;
public class Square
{
public static void main(String[] args)
{
int row = 1;
int column = 1;
int side;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the size of the square side: ");
side = input.nextInt();
while (row <= side)
{
column = 1;
System.out.print("*");
while (column <= (side - 2))
{
System.out.print(row == 1 || row == side ? "*" : " ");
++column;
}
++row;
System.out.printf("*%n");
}
}
}
对于for循环,没有什么比while循环更有效的了。对于int x=0;xintx=0;当x
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System.out.print("Enter the size of the side of a square: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
String re1 = repeat1("*", num);
String re2 = repeat2("*", num);
System.out.println(re1);
System.out.print(re2);
System.out.print(re1);
}
public static String spaces(int i) //Repeat space
{
String spaces = "";
for (int j = 1; j <= (2 * i - 3); j++) //(2 * i - 3) : for exrta space between Asterisks on Top and bottom side
{
spaces = spaces + " ";
}
return spaces;
}
public static String repeat1(String s, int i) //Top and bottom side
{
String tst = "";
for (int j = 1; j <= i; j++)
{
tst = s + " " + tst;
}
return tst;
}
public static String repeat2(String s, int i) // Left and right side
{
spaces(i);
String tst1 = "";
for (int j = 1; j <= i - 2; j++)
{
tst1 = s + spaces(i) + s + "\n" + tst1;
}
return tst1;
}
}
// Square.java
// Create a hollow square of asterisks.
// The side size is entered by the user
import java.util.Scanner;
public class Square
{
public static void main(String[] args)
{
int row = 1;
int column = 1;
int side;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the size of the square side: ");
side = input.nextInt();
while (row <= side)
{
column = 1;
System.out.print("*");
while (column <= (side - 2))
{
System.out.print(row == 1 || row == side ? "*" : " ");
++column;
}
++row;
System.out.printf("*%n");
}
}
}