2010-12-01 15 views
0

switch文に条件を追加するにはどうすればいいですか(例:平均マークの等級を表示)switch文

+8

もう少し詳しく教えてください。たとえば、何を試してみましたか、どのようなアウトプットを得ようとしていますか? – psmears

+0

または、あなたは何のように見えますか? –

+0

関連:http://stackoverflow.com/questions/3786358/get-rid-of-ugly-if-statements – BalusC

答えて

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

switch文の中に '<' or '>'マークを追加できません。 – athila

+0

このコードは長すぎます..そうでしょうか? :) – athila

+0

@athila:いいえ、あなたは平等だけを比較できます。基本的に 'switch(var){case SOME_VALUE:/ * code * /}'は 'if(var == SOME_VALUE)'と全く同じです。ここで等価演算子を変更することはできません。 –

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は、何と等しくありませんでした!

2番目のケースでbreakキーワードを使用しなかったため、3番目の実行には2行(1つは正しくないもの)があることに注意してください。

は、今の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文に使用する文字数よりも小さいものを使用します。以下はactionscript 3にあります...

var unknown1:number = 8;

var unknown2:Number = 2;

var lowerBoundary = 1;

VAR upperBoundary = 5

スイッチ(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; 

}

出力... 値は、この例では境界

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文です。