2010-12-01 124 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語句,它根據隨機選擇的數字來做不同的事情。