Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Java (https://www.askmehelpdesk.com/forumdisplay.php?f=440)
-   -   I would like to know How to Print a square with asterisks in java with while loop onl (https://www.askmehelpdesk.com/showthread.php?t=776947)

  • Nov 28, 2013, 04:46 PM
    amratif
    I would like to know How to Print a square with asterisks in java with while loop onl
    Quote:

    i would like to know How to Print a square with asterisks in java with while loop only but with out for loop and with out any logic operations like: and or Or this code something like that if we enter 4 it should print
    * * * *
    * *
    * *
    * * * *
    but it do some thing else like that

    * **


    *
    Code:

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

  • Nov 29, 2013, 07:15 AM
    Scleros
    Your algorithm is fundamentally flawed if you are intending to print out each line (X axis) in sequence (Y axis). Scanner objects also need some housekeeping done when finished with them.

  • All times are GMT -7. The time now is 12:03 PM.