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

Thread: Switch in a For loop

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Switch in a For loop

    		for(int r=25;r<=125;r+=25)
    		{
    			int color = 1;
    			switch(color)
    			{
     
    				case 1 : g.setColor(Color.black);  Expo.fillCircle(g,625,175,r); color++; break;
    				case 2 : g.setColor(Color.white);  Expo.fillCircle(g,625,175,r); color++; break;
    				case 3 : g.setColor(Color.blue);  Expo.fillCircle(g,625,175,r); color++; break;
    				case 4 : g.setColor(Color.red);  Expo.fillCircle(g,625,175,r); color++; break;
    				case 5 : g.setColor(Color.yellow);  Expo.fillCircle(g,625,175,r); color++; break;
    			}
     
     
    		}
    		g.setColor(Color.black); 	//reset colors.

    I am attempting to draw 5 concentric circles with increasing radius and chose the colors that each circle is filled with via a switch of an int. The result is a circle of maximus radius (which would indicate that the for loop is working, right?), but it is all the same color, the color of black which is case 1. What am I doing wrong, why won't the other circles change colors?


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Switch in a For loop

    You've declared and initialized your color variable inside of the for loop. So even though you increment it, it reverts back to 1 with every iteration of the loop.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    Levica (December 6th, 2012)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch in a For loop

    The code sets the switch variable's value to 1 every time through the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Levica (December 6th, 2012)

  6. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Switch in a For loop

    Thanks!! After changing the int declaration to before the for loop there was still a problem where the largest radius circle of the last color showed up, but I easily fixed that by changing r within the for loop. Thanks again!

  7. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Switch in a For loop

    	Random rand = new Random();
    		int color = 0;	//1 = black, 2 = white, 3 = blue, 4 = red, 5 = yellow
    		for(int r=125;r<=125;r-=25)		//execute loop as long as the radius is 125(the max value used) or less, and decrease radius of circles as executed.
    		{
    			color = rand.nextInt(5)+1;
    			switch(color)	//for every circle check the color integer to determine the color the circle is to be filled with.
    			{
     
    				case 1 : g.setColor(Color.black);  Expo.fillCircle(g,625,175,r); break;
    				case 2 : g.setColor(Color.white);  Expo.fillCircle(g,625,175,r); break;
    				case 3 : g.setColor(Color.blue);  Expo.fillCircle(g,625,175,r); break;
    				case 4 : g.setColor(Color.red);  Expo.fillCircle(g,625,175,r);  break;
    				case 5 : g.setColor(Color.yellow);  Expo.fillCircle(g,625,175,r);  break;
    			}
     
     
    		}
    		g.setColor(Color.black)

    I am attempting to generate random colors for each circle out of the 5 color options, and it works except if you wait long enough after the circle has been drawn, the window will begin flashing random colors incessantly. Why is this?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch in a For loop

    Why is this?
    Probably because of the way the program is coded.
    Can you make a small program that compiles, executes and shows the problem

    the window will begin flashing random colors incessantly.
    Can you explain? The code uses 5 colors, each in its own ring. Are you saying that there are more than 5 colors used?

    What does the method do with the variable: r? What values are given to r as the code executes?
    Add a println that prints out the value of r as the code executes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How do I loop a switch Statement?
    By Arkeshen in forum Java Theory & Questions
    Replies: 10
    Last Post: August 2nd, 2018, 07:47 AM
  2. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 01:28 PM
  3. Switch statement inside a for loop need help.
    By TheWhopper858 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 12th, 2011, 11:50 PM
  4. Do While loop + switch, Vowel Count
    By mwardjava92 in forum Loops & Control Statements
    Replies: 3
    Last Post: November 9th, 2011, 11:46 AM
  5. private method , loop , switch , help?
    By wolfgar in forum Loops & Control Statements
    Replies: 9
    Last Post: November 8th, 2009, 11:04 PM