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 3 of 3

Thread: For loop help!

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default For loop help!

    Hi,

    I'm new to Java and I have a homework assignment where I'm unable to do this code.

    Q.)Write a program called RectanglePattern.java with a main method. The program prompts for the height and width of a rectangle (you can assume that the user always enters a positive number), and prints out the corresponding number rectangle as shown in the sample run.

    Sample run of the program:

    Enter the width of the rectangle: 5
    Enter the height of the rectangle: 4

    1 1 1 1 1
    2 2 2 2 2
    3 3 3 3 3
    4 4 4 4 4

    So far I have this:

    import java.util.Scanner;

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

    System.out.print ("Enter the width of the rectangle : ");
    int width = sc.nextInt();

    System.out.print ("Enter the height of the rectangle : ");
    int height = sc.nextInt();

    for (int i = 1; i<=width; i++) {
    System.out.print ("1 ");

    }

    System.out.println (" ");
    for (int j = 1; j <=height; j++) {

    System.out.println ("2 ");
    }


    }


    }

    I figure there has to be a nested loop somewhere here, but I can't figure how.

    --- Update ---

    bump! (10char)


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: For loop help!

    There is never a reason to bump a post on this forum, there is no timeline hiding your post, especially after two minutes.
    Please use code tags when posting code, assistance can be found on the Announcements page.
    What are you stuck on?
    What is your question? ("I'm unable to do this code." is not a question)

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: For loop help!

    Print height rows (outer). For each print out width numbers (inner). In between each row the value that gets printed increases.
    Improving the world one idiot at a time!

Similar Threads

  1. help with when the for loop is met and i want to run the while loop again
    By m49er704 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 22nd, 2013, 09:03 AM
  2. 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
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 10:22 PM
  4. 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, 07:57 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM

Tags for this Thread