Java 用循环打印正方形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20238155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing a Square with loops
提问by user3040990
//college work
//大学工作
Hello, I am trying to print a square by using loops, it also require the user to input the height and width. The Output should look like this
您好,我正在尝试使用循环打印一个正方形,它还需要用户输入高度和宽度。输出应如下所示
.... . . ....
any help would be apprciated
任何帮助都会得到认可
import java.util.Scanner;
public class Ex1 {
public static void main(String[] args) {
Scanner input = new Scanner(System. in );
System.out.print("Please enter the height of the box: ");
int x = input.nextInt();
System.out.println("Please enter a width for the box: ");
int y = input.nextInt();
drawbox(x, y);
}
static void drawbox(int x, int y) {
for (int j = 0; j < y; j++) {
System.out.println("*");
System.out.println();
for (int i = 0; i < x - 2; i++) {
System.out.print("*");
for (int z = 0; z < y - 2; z++) {
System.out.print(" ");
}
System.out.println("*");
for (int i = 0; j < y; j++) {
System.out.println("*");
}
}
}
}
}
采纳答案by Masudul
Change your loop from
改变你的循环
for(int i = 0; j<y ; j++){
System.out.println("*");
}
To
到
for(int j = 0; j<y ; j++){
System.out.println("*");
}
To draw rectangle, change your drawbox code like:
要绘制矩形,请更改您的 drawbox 代码,例如:
static void drawbox(int x, int y) {
for (int i = 0; i < y; i++) {
System.out.print("*");
}
System.out.println();
for (int i = 0; i < x - 2; i++) {
System.out.print("*");
for (int j = 0; j < y - 2; j++) {
System.out.print(" ");
}
System.out.println("*");
}
for (int i = 0; i < y; i++) {
System.out.print("*");
}
System.out.println();
}
回答by Le Duy Khanh
change
改变
for(int i = 0; j<y ; j++){ System.out.println("*"); }
for(int i = 0; j<y ; j++){ System.out.println("*"); }
to
到
for(int i = 0; i<y ; i++){ System.out.println("*"); }
for(int i = 0; i<y ; i++){ System.out.println("*"); }
回答by Sionnach733
You could use this(see below). this will print *'s for the first and last row. for the middle rows, it prints a star followed by spaces and closes with a star. It also has another method allStars
which reuses code for first and last lines.
你可以使用这个(见下文)。这将为第一行和最后一行打印 *。对于中间的行,它打印一个星号,后跟空格并以星号结束。它还有另一种方法allStars
,可以重用第一行和最后一行的代码。
static void drawbox(int height, int width){
for (int i = 1; i <= height; i++) {
if(i==1 || i==height){
allStars(width);
}else{
System.out.print("* ");
for(int k = 2; k < width; k++){
System.out.print(" ");
}
System.out.print("*");
System.out.print("\n");
}
}
}
static void allStars(int width){
for(int k = 1; k <= width; k++){
System.out.print("* ");
}
System.out.print("\n");
}
回答by Ruchira Gayan Ranaweera
Your solution was good enough when issue fixed. But take this as easy way, hope you will get some idea from this
问题解决后,您的解决方案已经足够好了。但是把这个当作简单的方法,希望你能从中得到一些想法
private static void printSquare(int width,int length){
for(int i=0;i<width;i++){
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append("* ");
for (int j=2;j<length;j++){
if(i==0){
stringBuilder.append("* ");
}else if(i==width-1){
stringBuilder.append("* ");
}else {
stringBuilder.append(" ");
}
}
stringBuilder.append("* ");
System.out.println(stringBuilder);
}
}
When printSquare(5,5);
, Out put
当printSquare(5,5);
, 输出
* * * * *
* *
* *
* *
* * * * *
回答by Frank Ngai
A simple algorithm to draw square by loops
一种循环绘制正方形的简单算法
public void draw(int length){
for(int i = 0; i < length; i++){
for(int j = 0;j < length; j++){
if(i!= 0 && j > 0 && i!= length - 1 && j!= length-1){
System.out.print(" ");
}
else System.out.print("*");
}
System.out.println();
}
}
回答by Jim Cyril
Try this one:
试试这个:
import java.util.Scanner;
public class Rectangle {
public static void main(String args[]){
Scanner input = new Scanner(System. in );
System.out.print("Please enter the height of the box: ");
int x = input.nextInt();
System.out.print("Please enter a width for the box: ");
int y = input.nextInt();
drawbox(x, y);
}
static void drawbox(int x, int y) {
for (int j = 0; j < y; j++) {
System.out.print("* ");
}
System.out.println();
for (int i = 0; i < x; i++) {
System.out.print("* ");
for (int z = 0; z < y - 2; z++) {
System.out.print(" ");
}
System.out.println("*");
}
for (int k = 0; k < y; k++) {
System.out.print("* ");
}
}
}
Output:
输出:
Please enter the height of the box: 10
Please enter a width for the box: 10
* * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * *