Joe Mobarak
Joe Mobarak

Reputation: 21

Rectangle with Asterisks Java

I want to draw a Rectangle using asterisks. Actually i have started something but don't know how to continue how to go backwards you can go down by using println but how to go up Any help will be nice

import java.util.Scanner;

public class Shapes 
{
    public static void main (String [] args)
    {
         Scanner Read=new Scanner(System.in);

         int i;
         int j;
         int W;
         int L;

         System.out.println("Enter the Rectangle Width then Lenghts");

         W=Read.nextInt();
         L=Read.nextInt();

         for (i=0;i<W;i++)
         {
             System.out.println("*");
         }

         for (j=0;j<L;j++)
         {
             System.out.print("*");
         }
         System.out.println("");    
    }   
}

Upvotes: 0

Views: 1454

Answers (1)

Sujal Mandal
Sujal Mandal

Reputation: 1029

I will give you an idea.

take a number n, the number of asterisks.

  • Iteration 1: In the first line print n number of asterisks

  • iteration 2: print one * in the left and n-2 spaces consecutively and one more * in the right

  • iteration 3: same as iteration 2
  • iteration 4: same as iteration 3
  • iteration (n-1) : same as above
  • iteration n: same as iteration 1

Only thing is you have to adjust for the newline spacings because the end result might not look like a rectangle despite of having equal number of asterisks.

Upvotes: 4

Related Questions