开发者

Switch statement

How can I add conditions into a switch statement?(ex:-Displaying the grade for the 开发者_JAVA百科average marks)


I recommend using if-else... switch statements can only compare on equality.

With an integer score, you COULD do something like...

switch (score)
{
  case 100:
  case 99:
  case 98:
  case 97:
  case 96:
  case 95:
  case 94:
  case 93:
  case 92:
  case 91:
  case 90:
    grade = 'A';
    break;
  case 89:
    /* ... */
}

See the problem? :-)


You can't. Use an if-else-if-else.


Here is how I use less than greater than in a switch statement. The following is in actionscript 3...

var unknown1:Number = 8;

var unknown2:Number = 2;

var lowerBoundary = 1;

var upperBoundary = 5

switch(true){

case (unknown2 < lowerBoundary || unknown2 > upperBoundary):
    trace("value is out of bounds");
break;

case (unknown2 > lowerBoundary && unknown2 < upperBoundary):
    trace("value is between bounds");
break;

default:
    trace("Out of Luck");
break;

}

Output... value is between bounds


This question is listed with a Java tag so...

Generic switch statement:

// ... within class scope
private final int CONSTANT_1 = 1;
private final int CONSTANT_2 = 2;
private final int CONSTANT_3 = 3;
// ... 
public void doStuff(MyObject myObject){
   int variable = myObject.getIntValue();
   switch(variable){
   case CONSTANT_1:
      System.out.println(variable + " is equal to " + CONSTANT_1);
      // use a break statement to tell the switch to stop here
      // or else it will execute all subsequent cases:
      break;
   case CONSTANT_2: 
      System.out.println(variable + " is equal to " + CONSTANT_2);
      // what happens if I leave out the break?
   case CONSTANT_3:
      System.out.println(variable + " is equal to " + CONSTANT_2);
      break;
   default:
      System.out.println(variable + " wasn't equal to anything!");
}

Let's say I run through this 3 times and "myObject.getIntValue()" returns these values in this order; 3, 1, 2, and finally 42. Then the following output would be generated: First time through using the value '3'...

3 is equal to 3

Second time through using the value '1'...

1 is equal to 1

Third time through using the value '2'...

2 is equal to 2
2 is equal to 3

Fourth time through using the value '42' ...

42 wasn't equal to anything!

Notice the third run has two lines (and one incorrect one) because I left out the break keyword for the second case.

Now in Java 1.5 and up, you can also switch on the Enum type:

public void doStuff(MyObject myObject){
   MyEnumType varType = myObject.getEnum();
   switch(varType){
   case MyTypeOne:
   // everything else is the same -- nothing special here.
   // put whatever code you want in.
      break;
   case MyTypeTwo:
   // everything else is the same -- nothing special here.
   // put whatever code you want in.
      break;
   case MyTypeThree:
   // everything else is the same -- nothing special here.
   // put whatever code you want in.
      break;
   default:
        // code for unknown case goes here
   }
}


Depending on what your ranges are you can use a formula. e.g.

switch(score/10) {
  case 10: case 9: case 8: return 'A';
  case 7: return 'B';
  case 6: return 'C';
  case 5: return 'D';
  default: return 'U';
}


In this example, does the code generate a random number and do something if it's that number or that number.

int num;   //Just declares a variable 
Random r = new Random();   //This makes an object that generates random numbers.
num = r.nextInt(2);    //This "Choose" the random number. The possible numbers are 0 and 1. and the sets the the num variable to the number.  

switch(num){   

case 0:     //This says if the number is 0 then do this.
//put code here.
break;

case 1:    //This says if the number is 1 then do this.
//put code here
break;
}

And this is a switch statement that do different things based on the number that randomly gets chosen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜