如何仅使用while循环在java中打印带星号的正方形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20276125/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:47:31  来源:igfitidea点击:

How to Print a square with asterisks in java with while loop only?

javaif-statementwhile-loop

提问by user3043998

I would like to know how to print a square with asterisks in java with a whileloop only but with out forloop and with out any logic operations like: ANDor OR

我想知道如何在 java 中打印一个带星号的正方形,while只有一个循环,但没有for循环,没有任何逻辑运算,如:ANDOR

This code should do something like that: if we enter 4it should print:

这段代码应该做这样的事情:如果我们输入4它应该打印:

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

but it does some thing else, like that:

但它会做一些其他的事情,比如:

* **


*  


    Scanner input = new Scanner(System.in);

    int squareside ;
    System.out.print("Enter the side: ");
    squareside = input.nextInt();

    while ( squareside != -1 )
    {
        int xaxis = 0 ;
        while ( xaxis < squareside )
        {

            if( xaxis == 0 ) {
                System.out.print(" *") ;
            }
            else if( xaxis == squareside - 1){
                System.out.print(" *") ;
            }
            else
                System.out.print(" ") ;

            xaxis ++ ;

        }
        int yaxis = 0 ;

        while( yaxis < squareside )
        {
            if( yaxis == 0 )
            { 
                System.out.print(" *");
            }
            else if( yaxis == squareside - 1)

            {
                System.out.print(" *");
            }
            else    
                System.out.print(" ");  

            yaxis ++;
            System.out.println();

        }

        System.out.print("Enter the side: ");
        squareside = input.nextInt();
    }

回答by yasen

You can notice that you have to print stars only on the border values. That is when (x or y) is 0 or (x or y) is equal to squareside-1.

您会注意到您只需要在边框值上打印星号。即当 (x 或 y) 为 0 或 (x 或 y) 等于 squareside-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();
}

回答by kiruwka

quick and verbose, but should work. It does not use ifor anything else except whileloop. Feel free to optimize :

快速而冗长,但应该有效。if除了while循环之外,它不使用或其他任何东西。随意优化:

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("* ");
    }
}

回答by gadolf

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;
    }
}

回答by DorinD

This is a simple solution to the problem. Spaces can be easily added between asterisks, if needed:

这是解决问题的简单方法。如果需要,可以轻松地在星号之间添加空格:

   // 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");
        }
     }
  }