Solved

Nested Switch statement in an If statement

Posted on 2001-07-26
5
367 Views
Last Modified: 2008-03-10
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!!
0
Comment
Question by:menreeq
  • 3
5 Comments
 
LVL 7

Accepted Solution

by:
tomboshell earned 100 total points
ID: 6321488
It would be in your for loop.  How big is the shapes array?  If it only has 2 entries (0 & 1) it will run only once.  The counter starts off at zero, goes through the loop and then compares the value again after being incremented (now it is at 1).  If there are only 2 entries in the array (ie. at the 0 and at the 1 indexies) it will quit the loop.  
Try modifying your code so that the comparison is "; i <= shapes.length;"

I am assuming that since you only had to case statements and no default.

Tom
0
 

Expert Comment

by:prakash_mas
ID: 6321497
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
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 6321681
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.
0
 

Author Comment

by:menreeq
ID: 6323014
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.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 6325821
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
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
pairs challenge 5 45
Java and GPO 11 47
micro services spring boot application error 3 34
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now