2010-12-01 123 views
0

如何将条件添加到开关语句中?(例如: - 显示平均分数的分数)开关语句

+8

你能提供一些更多的细节吗?例如,你尝试了什么,以及你想要获得什么输出? – psmears 2010-12-01 18:38:20

+0

或者,您希望构造看起来像什么? – 2010-12-01 18:40:52

回答

1

您不能。使用if-else-if-else。

6

我推荐使用if-else ... switch语句只能在平等上比较。

一个整数的分数,你可以不喜欢......

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: 
    /* ... */ 
} 

看这个问题? :-)

0

这个问题都列出了一个Java标签等等...

通用switch语句:

// ... 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!"); 
} 

比方说,我通过这个3倍,并运行 “myObject.getIntValue()”按此顺序返回这些值; 3,1,2,最后42。然后将产生以下输出: 第一次通过使用值 '3' ...

3等于3

第二时间通过使用值 '1' ...

1通过使用值等于1

第三时间 '2' ...

2 is equal to 2 
2 is equal to 3
通过使用值 '42'

第四次......

42不等于任何东西!

注意第三次运行有两行(和一个不正确的一行),因为我省略了第二种情况的break关键字。

现在的Java 1.5及更高版本,还可以切换对枚举类型:

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 
    } 
} 
1

取决于你的范围,你可以用一个公式。 例如

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'; 
} 
1

下面是我如何使用小于switch语句中的大于。以下是动作3 ...

var unknown1:Number = 8;

var unknown2:Number = 2;

var lowerBoundary = 1;

变种upperBoundary = 5

开关(真){

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; 

}

输出... 值为界限

0

在这个例子中之间,并在代码生成一个随机数字,如果是那个数字或那个数字,就做一些事情。

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; 
} 

这是一个switch语句,它根据随机选择的数字来做不同的事情。