Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Star Loop

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Star Loop

    import java.util.Scanner;
     
     
    public class loops {
    	public static void main(String[] args) {
    	 int length;
    	 int width;
    	int count = 0;			
    	Scanner input = new Scanner (System.in);
    System.out.print("Enter the length: ");
    length = input.nextInt();
    while (length<=0 || length >=21 ) {
    	System.out.print("Invalid – enter length between 1 and 20:");
    	System.out.print("\nEnter the length: ");
    	length = input.nextInt();
     
    }
    System.out.print("Enter the width: ");
     
    	width = input.nextInt();
     
    while (width<=0 || width >=21 ) {
    	System.out.print("Invalid – enter width between 1 and 20:");
    	System.out.print("\nEnter the width: ");
    	width = input.nextInt();
    	if (width>=1 || width <=20){
    		while (count<width){
    			System.out.print("*"); 
    			count++;}
    	 }
    	}
     
     
    	while (count<length){
    		System.out.print("\n*" + width); 
    		count++;}
     
     
    }
     
    }

    Hi how do i solve this. to make my answer equal to the length and width I inputted.

    example:

    Enter the length: 6
    Enter the width: 4
    ****
    ****
    ****
    ****
    ****
    ****

    I always get the length right, but I can't make the width working
    Last edited by poldz123; October 26th, 2012 at 10:01 PM.


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Star Loop

    try using 2 different variables for length and width..
    good advice is to debug it, and trace what's happening.

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. Drawing a Star of David
    By tryingtolearn in forum AWT / Java Swing
    Replies: 2
    Last Post: July 31st, 2012, 07:01 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  4. star design programs
    By prasansani in forum Java Theory & Questions
    Replies: 6
    Last Post: September 20th, 2011, 12:42 PM
  5. A Star Pathfinding algorithm optimization
    By randomcracker in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 18th, 2011, 11:11 PM