如何将条件添加到开关语句中?(例如: - 显示平均分数的分数)开关语句
Q
开关语句
0
A
回答
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语句,它根据随机选择的数字来做不同的事情。
相关问题
- 1. Perl开关语句
- 2. PHP开关语句
- 3. 开关语句性能C#
- 4. while循环,开关语句
- 5. 开关语句和类
- 6. 开关语句缩进
- 7. SRSS中的开关语句
- 8. 开关与if语句C#
- 9. 开关语句问题
- 10. 开关语句不好?
- 11. 凝聚开关语句?
- 12. 虽然,开关,case语句
- 13. 开关语句在php
- 14. 多条件开关语句?
- 15. 开关语句 - 变量“case”?
- 16. 阵列开关case语句
- 17. 大开关语句:坏OOP?
- 18. csh中的开关语句
- 19. 多参数开关语句
- 20. Lisp中的开关语句
- 21. 内部开关语句mpdf
- 22. 关于开关条件语句
- 23. 简化开关语句(Objective-C)
- 24. Java开关语句“case,default expected”
- 25. 开关语句不被识别
- 26. 随机数和开关语句
- 27. 为什么开关语句在案例
- 28. 替代开关/ if else语句?
- 29. C - 开关语句 - 输出差异
- 30. 开关语句总是默认在javascript
你能提供一些更多的细节吗?例如,你尝试了什么,以及你想要获得什么输出? – psmears 2010-12-01 18:38:20
或者,您希望构造看起来像什么? – 2010-12-01 18:40:52