Results 1 to 11 of 11
  1. #1
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Unhappy Program - Print row of 5 even numbers, 5 rows, last num 50

    Hi guys, I'm new to Java and have an AP comp sci class. I have an assignment to use "for" loops to complete a goal.

    We're just on the basics right now so please try to show me the simplest way of how I'm supposed to put this program together.

    Basically, it needs to print out 5 rows of even numbers. The result has to be like this:
    2 4 6 8 10
    12 14 16 18 20
    22 24 26 28 30
    32 34 36 38 40
    42 44 46 48 50

    Edit: It needs to use for loops and they can be nested.

    I know it's probably an easy logic to think of, but my brain is functioning properly and I've tried a 100 different combinations of for loops that didn't' work. And that has made me really frustrated that I can't figure out this simple looking problem.

    I would really appreciate if you guys could help me solve this.
    Last edited by An Alien; 12-20-2010 at 12:37 PM. Reason: Add something

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by An Alien View Post
    Hi guys, I'm new to Java and have an AP comp sci class. I have an assignment to use "for" loops to complete a goal.

    We're just on the basics right now so please try to show me the simplest way of how I'm supposed to put this program together.

    Basically, it needs to print out 5 rows of even numbers. The result has to be like this:
    2 4 6 8 10
    12 14 16 18 20
    22 24 26 28 30
    32 34 36 38 40
    42 44 46 48 50

    Edit: It needs to use for loops and they can be nested.

    I know it's probably an easy logic to think of, but my brain is functioning properly and I've tried a 100 different combinations of for loops that didn't' work. And that has made me really frustrated that I can't figure out this simple looking problem.

    I would really appreciate if you guys could help me solve this.
    We're not supposed to do your homework for you here, but we can help you by answering questions for you and help you debug. Let's see what you've tried first, as this way we can see what you know, what you don't know, what needs clarification, etc.

    Best of luck, and welcome to the forum!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by An Alien View Post
    Hi guys, I'm new to Java and have an AP comp sci class. I have an assignment to use "for" loops to complete a goal.

    We're just on the basics right now so please try to show me the simplest way of how I'm supposed to put this program together.

    Basically, it needs to print out 5 rows of even numbers. The result has to be like this:
    2 4 6 8 10
    12 14 16 18 20
    22 24 26 28 30
    32 34 36 38 40
    42 44 46 48 50

    Edit: It needs to use for loops and they can be nested.

    I know it's probably an easy logic to think of, but my brain is functioning properly and I've tried a 100 different combinations of for loops that didn't' work. And that has made me really frustrated that I can't figure out this simple looking problem.

    I would really appreciate if you guys could help me solve this.
    Think of a little grid of size 5x5 where x goes from left to right 0,1,2,3 and 4 and y is running down over the same numbers 0,1,2,3 and 4. The leftmost number in each row is 10*y+2 (check that) and each number to the right increments the previous number with 2 so each number in each row is 10*y+2+2*x (also check that). Creating two nested loops where both x and y iterate over the numbers 0,1,2,3 and 4 isn't rocket science.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    I just tried a million other combinations that didn't work. So, I'm doing it step by step now.

    So far, I've got this:
    for (int x = 2; x <= 50; x+=2){

    System.out.print(x + " ");

    }
    And this prints out:
    2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50

    But it's a single line D:

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The solution to that is to do as Jos suggested: don't use one for loop but two for loops, one nested inside of the other. Your System.out.println can go after the inner loop.

  6. #6
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    I'm sorry, I just don't understand the way Jos worded it. I feel so stupid right now.

    Another failed combination:
    for (int x = 1; x <= 5; x++){
    for (int y = 1; y <=5; y++){
    System.out.print(x+y + " ");
    }
    System.out.println(" ");
    }

  7. #7
    Fortu is offline Member
    Join Date
    Dec 2010
    Posts
    57
    Rep Power
    0

    Default

    Java Code:
    for (int x = 1; x <= 5; x++)
    {
      for (int y = 1; y <=5; y++)
      {
        System.out.print(x+y + " ");
      }
      System.out.println(" ");
    }
    Alright, let's see.. As far as I see, I don't know why this wouldn't work-Ahh wait. You should be getting the output:

    2 3 4 5 6
    3 4 5 6 7
    4 5 6 7 8
    5 6 7 8 9
    6 7 8 9 10

    Now, you have the right idea with this code, but your error is in your x and y variables. You are declaring your x and y variables inside the two for loops. Now, if you think about the logic of your code, it is going through the for loop with the x variable 5 times, and each time it loops it is also going through the for loop with the y variable.

    One of the issues is that you declare the y int variable each time the second for loops starts up, so y is set back to 1 every time the first for loop (with the x variable) completes a loop and gets back to going through the loop again.

    You can use your y variable to help with the outputting, but try considering using your x to calculate one part of the number you want to output, y to calculate another part, and then adding the two values together to get the number you want.
    Last edited by Fortu; 12-19-2010 at 11:52 PM.

  8. #8
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by Fortu View Post
    but try considering using your x to calculate one part of the number you want to output, y to calculate another part, and then adding the two values together to get the number you want.
    I know I have to use the x and y to calculate. I just can't think of how I should calculate those numbers.

  9. #9
    Join Date
    Dec 2008
    Location
    Manchester England
    Posts
    14
    Rep Power
    0

    Default

    Another way to crack the problem is to use 1 loop. Then use the modular operator to check for factors of 5. This would make the maths you are computing more readable. Something like

    Java Code:
    int y; 
           for (int x = 1; x <= 25; x++)
           {
                y = x * 2; 
                System.out.print(y+" "); 
                if (x % 5 == 0) 
                    System.out.println("");
    
           }
    Note the print as well as the println method.
    Good luck on your way to the top of the class soldier, at least you made an effort.

  10. #10
    Join Date
    Dec 2008
    Location
    Manchester England
    Posts
    14
    Rep Power
    0

    Default

    Another way to crack the problem is to use 1 loop. Then use the modular operator to check for factors of 5. This would make the maths you are computing more readable. Something like

    Java Code:
    int y; 
           for (int x = 1; x <= 25; x++)
           {
                y = x * 2; 
                System.out.print(y+" "); 
                if (x % 5 == 0) 
                    System.out.println("");
    
           }
    Note the print as well as the println method.
    An alternative would be to do away with the y variable such as
    Java Code:
     for (int x = 1; x <= 25; x++)
           {
                System.out.print( x*2 + " ");
                if (x % 5 == 0) 
                    System.out.println("");
    
           }

  11. #11
    An Alien is offline Member
    Join Date
    Dec 2010
    Posts
    20
    Rep Power
    0

    Default

    Wow, it works. I've never used the remainder operator before so I never thought of doing it that way. Thanks

Similar Threads

  1. Replies: 6
    Last Post: 12-08-2010, 12:39 AM
  2. print random numbers without repetition
    By princess.blue in forum New To Java
    Replies: 3
    Last Post: 02-04-2010, 09:37 AM
  3. how to multiply numbers in rows and print it next to it
    By racewithferrari in forum New To Java
    Replies: 1
    Last Post: 01-16-2010, 06:24 PM
  4. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM
  5. prime numbers program
    By i contra i in forum New To Java
    Replies: 9
    Last Post: 01-15-2009, 07:22 AM

Posting Permissions

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