switch文に条件を追加するにはどうすればいいですか(例:平均マークの等級を表示)switch文
switch文
答えて
できません。 if-else-if-elseを使用します。
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:
/* ... */
}
は、問題を参照してください? :-)
この質問はとても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
}
}
あなたの範囲はあなたが数式を使用することができます内容に応じて。例:
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';
}
ここでは、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;
}
出力... 値は、この例では境界
間にある、コードが生成しません乱数を入力し、その番号またはその番号の場合は何かをします。
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文です。
- 1. switch文
- 2. C++のswitch文
- 3. C#のswitch文
- 4. switch文8
- 5. switch文のヘルプ
- 6. は、switch文
- 7. PHP switch()文
- 8. Javaのswitch文
- 9. C++ fizzbuzz switch文?
- 10. switch文のブレークメニュー
- 11. PHPのswitch文
- 12. Java switch文
- 13. c switch文
- 14. switch文とisset
- 15. switch文C++
- 16. Javascript switch文?
- 17. switch文のif文をwithからswitch文に変更する
- 18. PHPのswitch case文
- 19. ガスx86の:switch文
- 20. PHP switch文のエラー
- 21. JavaScriptのswitch文は
- 22. switch文のJacocoカバレッジ
- 23. リファクタリングjava switch文(ブラックジャック)
- 24. switch文中のmpdf
- 25. C++ switch文のエラー
- 26. Objective-Cのswitch文
- 27. Javascript ifとswitch文
- 28. EXC BADアクセスswitch文
- 29. groovy switch文のリストマッチング
- 30. 私のswitch文のエラーメッセージ?
もう少し詳しく教えてください。たとえば、何を試してみましたか、どのようなアウトプットを得ようとしていますか? – psmears
または、あなたは何のように見えますか? –
関連:http://stackoverflow.com/questions/3786358/get-rid-of-ugly-if-statements – BalusC