2 Replies - 537 Views - Last Post: 29 June 2009 - 09:25 PM Rate Topic: -----

#1 AlphaOmegas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-September 08

Switch statement problem

Posted 29 June 2009 - 02:53 PM

class Advice {

	final int LITTLE_ADVICE = 0;
	final int MORE_ADVICE = 1;
	final int LOTS_OF_ADVICE = 2;

	void giveAdvice(int selection) {
		switch (selection) {
			case LITTLE_ADVICE:
				System.out.println("See no evil");

			case MORE_ADVICE:
				System.out.println("Speak no evil");

			case LOTS_OF_ADVICE:
				System.out.println("Hear no evil");

			default:
				System.out.println("Give no advice");
		}
	}
}

class TestAdvice {

	public static void main(String arg[]) {
		Advice A = new Advice();
		A.giveAdvice(0);
	}
}


i cant get it to only display all of the project instead on one

*Edited for a more descriptive topic title

This post has been edited by pbl: 29 June 2009 - 09:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Switch statement problem

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Switch statement problem

Posted 29 June 2009 - 02:58 PM

Use the break keyword to end your case's.
Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Switch statement problem

Posted 29 June 2009 - 09:25 PM

Switch 101

int x = 0;
switch(x)
{
case 0 : System.out.println("0"); 
case 1 : System.out.println("1");
case 2 : System.out.println("2");
default : System.out.println("Invalid Selection");
}


In switch statements, if it doesn't meet the statement it will try the next one. If it doesn't meet any of the statements, it wont do anything unless you have a default

When it does meet a case, it will perform everything in the case and do 1 of 2 things.

1. Continue onto the next case until the last case or hit a break

2. Will do only that case and then hit a break

break <-- break is used to stop a method from continuing. You could use a break in a for loop, that is generally bad programming to do so but yeah, it is possible. Break is as what is sounds like, a stop, halt, "advance no further".

Right now, it will find the first case to be true, and thus do the println, then continue to the rest of the cases.

Change it to this though: case 0 : System.out.println("0"); break;
Once it finishes the first case it will stop the switch statement and only print out "0"

Change it to this though: case 1 : System.out.println("1");break
It will print out the first case, and then continue to the second one and print out 1 then it will hit the break and exit the switch.

Change "case 0" to "case 10", and do the switch, it will go through each case and do none of them. Therefore it reaches the default, that is basically saying "if you didnt get chosen come with me". It will perform the actions inside the default case and then exit the switch statement.


Hope this helps!

This post has been edited by Fuzzyness: 29 June 2009 - 09:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1