Results 1 to 2 of 2
  1. #1
    amratif is offline Member
    Join Date
    Nov 2013
    Posts
    1
    Rep Power
    0

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

    I would like to know how to print a square with asterisks in java with a while loop only but with out for loop and with out any logic operations like: AND or OR

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

    *-*-*-*
    *------*
    *------*
    *-*-*-*
    but it does some thing else, like that:

    * **


    *
    note this ( - ) mean space i put it to be easy in reading

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

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,616
    Rep Power
    5

    Default Re: Post How to Print a square with asterisks in java with while loop only ?

    You need to break it up into pieces. First focus on just printing the top side correctly for any value. Once you have that done you also have the bottom done. Then just focus on the sides.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 11-06-2013, 12:30 AM
  2. post increment inside a loop?
    By brendan89 in forum New To Java
    Replies: 2
    Last Post: 02-24-2012, 06:40 PM
  3. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 01:21 PM
  4. Perfect Square Array Input Using For Loop
    By dalangley in forum New To Java
    Replies: 9
    Last Post: 01-27-2009, 01:33 AM
  5. 8-Square puzzle loop
    By SapphireSpark in forum New To Java
    Replies: 7
    Last Post: 12-04-2008, 07:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •