Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    48
    Rep Power
    0

    Question Need help with a program that outputs sets of even numbers


    Code:
    public class EvenNumberSets
    {
        public static void main(String[] args) 
        {
            int twos = 0;
            int count = 0;
            int result = 0;
            int everyThird = 0;
        	
        	System.out.println ("This program will output all numbers from 2 through 20 in incriments of 2.");
        	System.out.println ();
        	
        	do
        	{
        		result = result + 2;
        		
        		while (everyThird < 3)
        		{
        			twos = twos + 2;
        			System.out.println (twos);
        			result = twos;
        			everyThird++;
        		}
        		
        		count = count + 2;
        		
        	} while (count <= 32); // end of loop
        	
        	System.out.println ();	
        }
    }
    I'm trying to teach myself Java, and I ran across this problem. It sounds really simple... All I need to do is output numbers like so: 2, 4, 6, 10, 12, 14, 18, 20, 22 and so forth. You see it is counting by two, but skipping the fourth number. I coded the program shown above, but no matter what I try all I get is 2, 4, 6. I ran though it a few times, and my logic appears to be correct. But obviously I'm doing something wrong. So I could use a fresh pair of eyes on this and some advice.

    Appreciate it!
    Deathbliss
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2006
    Location
    True
    Posts
    873
    Rep Power
    111
    It is because you are doing all that WHILE everythird is smaller than three, and each time you add to everythird... so the first time, the result is 2, then everythird++ makes everythird = 1. Then the result is 4 and everythird = 2. Then the result is 6 and everythird = 3. Now look at your loop, "while everythird is smaller than 3" but now everythind is EQUAL to 3 so it stops. So if you want it to output 12 you will say while everythird is smaller than 6
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    48
    Rep Power
    0
    Well I'm trying to get it to output 2, 4, 6 skip 8, then 10, 12, 14, skip 16, then 18, 20, 22, and so on. That's why that everyThird nested loop is there. I'm not sure your answer will allow the program to do what I'm trying to get it to do.. If it does I guess I'm just not seeing what you're saying clearly enough and I need more explanation. But I do appreciate your response.
    - Deathbliss
  6. #4
  7. AYBABTU
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Here or There
    Posts
    1,256
    Rep Power
    380
    Problem is that you forget to reset everyThird when it reaches 3. You also do not need an inner loop to accomplish this. The following loop does what you describe:[hl=java]int everyThird = 1;
    for (int twos = 2; twos <= 20;twos += 2) {
    if (everyThird != 4) {
    System.out.println(twos);
    everyThird++;
    } else {
    everyThird = 1;
    }
    }
    [/hl]You'll (maybe) notice that this solution comes close to a description of what you want it to do in plain English.
    A common mistake people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
    Douglas Adams
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    48
    Rep Power
    0
    THANK YOU! With your hint at needing to reset everyThird I finally saw how to do this. Here's the code for anyone interested:

    Code:
    public static void main(String[] args) 
        {
            int twos = 0;
            
        	System.out.println ("This progam will output numbers in incriments of 2 from 2 through 30,");
        	System.out.println ("skipping every fourth number in the sequence.");
        	System.out.println ();
        	
        	for (int count = 1; count <= 4; count ++)
        	{
        		for (int everyThird = 1; everyThird <= 3; everyThird++)
        		{
        			twos = twos + 2;
        			System.out.println (twos);	
        		}
        		
        		twos = twos + 2;
        			
        	} // end of loop
        	
        	System.out.println ();
    I realized that not only did I have to reset the counter mechanism of everyThird, I also had to get count to iterate a number of times that cooresponded to the output I wanted. So if I wanted four sets of numbers counted by twos from 2 through 30 I would have to set counter to 1 and less than or equal to 4, or 0 and less than 4. I chose the first option for specificity. The result I believe is a very understandable but compact piece of code, which I could even easily adapt to accepting user input.

    Problem solved - thanks again!
    - Deathbliss
  10. #6
  11. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    Just for fun, here's another option that closely models the requirements description:
    Java Code:
    for (int i = 2, count = 0; i <= 30; i += 2) {            
        if (++count % 4 == 0) continue;
        System.out.println(i);
    }
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  12. #7
  13. AYBABTU
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Here or There
    Posts
    1,256
    Rep Power
    380
    While we're at it, why not loose the count variable?[hl=java]for (int i = 2; i <= 30; i += 2) {
    if (i % 8 == 0) continue;
    System.out.println(i);
    }[/hl]
    A common mistake people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
    Douglas Adams
  14. #8
  15. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    While we're at it, why not loose the count variable?
    Readability, IMHO. To me, it's harder to see the intent behind a mod 8 in the context of "count from 2 to 30 by twos, skipping every fourth number". I can look at the code in reply #6 and tell you right away what the output would be. I have to think too much about the code in reply #7.

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  16. #9
  17. AYBABTU
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Here or There
    Posts
    1,256
    Rep Power
    380
    If we're talking readability, you could consider writing a method to determine the fourth (or whateverth) number:[hl=java]for (int i = 2; i <= 30; i += 2) {
    if (isAtPosition(i, 4)) continue;
    System.out.println(i);
    }

    ...
    boolean isAtPosition(int n, int position) {
    return n % (2*position) == 0;
    }[/hl]
    But you're right, considering reply #6 and #7, yours is more readable.
    A common mistake people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
    Douglas Adams
  18. #10
  19. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    But you're right, considering reply #6 and #7, yours is more readable.
    How 'bout this?

    Code:
    for(int i=0x00;i<0x1E;)System.out.printf((((1<<++i)&0xEEEE)==0)?"":"%d\n",i*2);

    * cue CJ and her Unicode escapes...

    ~

    Comments on this post

    • wsa1971 agrees : That's worth some rep.
    • crownjewel82 agrees : :D
    • gimp agrees
    Last edited by Yawmark; January 22nd, 2007 at 08:09 AM.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  20. #11
  21. AYBABTU
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Here or There
    Posts
    1,256
    Rep Power
    380
    Originally Posted by Yawmark
    How 'bout this?

    Java Code:
    for (int i=0x01;i<0x1E;i++) System.out.printf((((1<<i)&0xEEEE)==0)?"":"%d\n", i*2);



    * cue CJ and her Unicode escapes...

    ~
    Much, much better... I mean, you can instantly see what the meaning behind that is without having to think one second.
    I have absolutely no clue how that actually works, but I don't want to sound stupid
    A common mistake people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
    Douglas Adams
  22. #12
  23. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    I have absolutely no clue how that actually works
    It's just a little bit of bit-fiddling; nothing special, really. I saw something similar a few years ago, and thought the technique was clever. It's not something I can ever see myself actually using for anything other than "entertainment", but it was "fun" to work out what was going on...

    ~
    Last edited by Yawmark; January 22nd, 2007 at 11:53 AM.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  24. #13
  25. AYBABTU
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Here or There
    Posts
    1,256
    Rep Power
    380
    Originally Posted by Yawmark
    It's just a little bit of bit-fiddling; nothing special, really. I saw something similar a few years ago, and thought the technique was clever. It's not something I'd ever use, I don't believe, but it was "fun" to work out what was going on...

    ~
    You know, there was a reason why I put that in small print. Dammit, now everybody knows I'm not as smart as you.

    I actually figured as much, but couldn't look at it long enough because my eyes started to bleed
    A common mistake people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
    Douglas Adams
  26. #14
  27. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    Chicago, IL
    Posts
    10,131
    Rep Power
    5058
    Dammit, now everybody knows I'm not as smart as you.
    Humility's a virtue, but you're overdoing it...

    ~
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}
  28. #15
  29. No Profile Picture
    rebel with a cause
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2004
    Location
    The Batsh!t Crazy State.
    Posts
    5,814
    Rep Power
    3466
    Originally Posted by Yawmark
    How 'bout this?

    Code:
    for(int i=0x00;i<0x1E;)System.out.printf((((1<<++i)&0xEEEE)==0)?"":"%d\n",i*2);

    * cue CJ and her Unicode escapes...

    ~
    Reminds me of that one line of perl that finds the root of a method, does the dishes, cuts the grass, and makes dinner.
    Dear God. What is it like in your funny little brains? It must be so boring.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo