Link to home
Start Free TrialLog in
Avatar of menreeq
menreeq

asked on

Nested Switch statement in an If statement

Hello...new java programmer here...with what i am sure is a simple question.

here is my code:
  for(int i=0; i < shapes.length; i++)
    {
      int x = (int)((Math.random()*2) + 1);
      pt1 = new Point((int)(Math.random()*maxPt), (int)(Math.random()*maxPt));
      pt2 = new Point((int)(Math.random()*maxPt), (int)(Math.random()*maxPt));
      pt1.show();
     
      switch(x)
        {
          case 1:
            shapes[i] = new Line(pt1, pt2);
            break;
          case 2:
            shapes[i] = new Circle(pt1, ((int)(Math.random()*maxRd)));
            break;
        }
    }

OK...so when i print out the results it looks like it runs through the 3 statements above the switch statment before it enters the switch statement.  I dont understand why.  I was expecting it to go through each line including the switch statement and then return to the first line again and complete the loop...What is it that i dont understand about this?

Thanks!!
ASKER CERTIFIED SOLUTION
Avatar of tomboshell
tomboshell

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prakash_mas
prakash_mas

tell me what i have under stand fromm u r question is correct or not , if i am correct go through the possible solution also

what i understood was , u are expecting  u r program to show the pt1 and to enter the
switch statement to do some action ( drawing coircle or line ) and continmue in the for loop
until the shapes.length.

possible proble may be
1)  x value may not be 1 Or 2 so it will not do any thing in switch ( use default optionin switch to check value of x)
2) some times break in the switch will make u to come out of the loop(  cover switch in extra brackets{})
3) check for length of shape also .

Rgds,
Prakash
Hey, prakash_mas very nice of you to lock it in such proper form and ediquite.  Very nice to know that you are beyond 100% sure of your answer and did not re-cover anything others have fairly posted as a comment.  Nice working in such a co-operative atmosphere.  

Thanks for the fairness.
Avatar of menreeq

ASKER

Shortly after posting my question and before the first reply was posted I deleted the question...I have no idea why it remained posted.  Actually the correct answer to my question would be that the code should work as I expected to and that there are no errors...the only problem was in the way I was interpreting the results...but there was nothing wrong with the coding.

I suppose that the fair thing would be to give the first person who answered this question the points...sorry for the drama.
Well, thanks.  Sorry that I couldn't actually been of help to you.  From the code sample that was given, that was the only thing I could think of for the code not working as expected.  But then I guess everything is all cool.

Tom