Ask Experts Questions for FREE Help !
Ask
    amratif's Avatar
    amratif Posts: 1, Reputation: 1
    New Member
     
    #1

    Nov 28, 2013, 04:46 PM
    I would like to know How to Print a square with asterisks in java with while loop onl
    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();
        }
    Scleros's Avatar
    Scleros Posts: 2,165, Reputation: 262
    Hardware Expert
     
    #2

    Nov 29, 2013, 07:15 AM
    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.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Java netbeans program- Connection between java and mysql,How to conn [ 0 Answers ]

which I wrote from a book but when I run it, it shows exception please give me the code to connect jlistbox to database when I click on btn1. btn coding should search "name" in database and shows all the names in listbox my coding: here-- r1,r2,r3 are radio btns and l1,l2,l3 are labels,...

Diagonal of a quare is the square root of 20 feet what is the area of the square? [ 2 Answers ]

The diaganol of a square is the square root of 20 feet what is the area of the square?

I am replacing the outside unit it is carrier 410a,can I replace the outside unit onl [ 0 Answers ]

I am replacing my carrier 410 carrier outside unit only can I replace with a r22 unit

I'm painting a room how many square meters in 1 square foot [ 1 Answers ]

I'm painting a room, I have mesured in square feet, but the paint tin gives the spread rate in square meters.I have 1 5 Liter tin. Question is how do I convert square meters into square feet.


View more questions Search