如何只使用switch...case来实现一个简单的数值范围划分

csofwood 2010-08-29 10:00:58
输入年龄,然后判断出这个人是少年、青年、还是老年

直接在main方法里实现,不能用if、for等,只能用switch...case。输入一个年龄int age,如果在0~17之间,则打印出少年;18~29打印青年;30~59中年;60~100老年

最好是大家自己的思路
...全文
232 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
csofwood 2010-09-02
[Quote=引用 3 楼 evangelionxb 的回复:]

Java code
import java.io.*;

public class tryCase1 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

……
[/Quote]
谢谢,我的跟你基本一样,下面是我第一次学编程时的思路,所以语法有点忘记了
switch (age>0?1:0)+(age>=18?1:0)+(age>=30?1:0)+(age>=60?1:0)+(age>100?1:0)
case (0): “精子”
case (1):“少年”
case (2):“青年”
case (3):“中年”
case (4):“老年”
case (5):“妖怪”
回复
evangelionxb 2010-09-01
import java.io.*;

public class tryCase1 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int age;


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
age = Integer.parseInt(br.readLine());

switch (age>0?(age>17?(age>29?(age>59?4:3):2):1):-1){
case (1):
System.out.println("少年");
break;
case (2):
System.out.println("青年");
break;
case (3):
System.out.println("中年");
break;
case (4):
System.out.println("老年");
break;
case (-1):
System.out.println("你输入的数值不合法");
break;

default:
System.out.println("default");
break;

}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

用嵌套的三元表达
我的120岁也能判定
回复
huntor 2010-09-01

public void testAge(int age){
switch(age){
case 0: System.out.println(" 0岁是少年!");break;
case 1: System.out.println(" 1岁是少年!");break;
case 2: System.out.println(" 2岁是少年!");break;
case 3: System.out.println(" 3岁是少年!");break;
case 4: System.out.println(" 4岁是少年!");break;
case 5: System.out.println(" 5岁是少年!");break;
case 6: System.out.println(" 6岁是少年!");break;
case 7: System.out.println(" 7岁是少年!");break;
case 8: System.out.println(" 8岁是少年!");break;
case 9: System.out.println(" 9岁是少年!");break;
case 10: System.out.println(" 10岁是少年!");break;
case 11: System.out.println(" 11岁是少年!");break;
case 12: System.out.println(" 12岁是少年!");break;
case 13: System.out.println(" 13岁是少年!");break;
case 14: System.out.println(" 14岁是少年!");break;
case 15: System.out.println(" 15岁是少年!");break;
case 16: System.out.println(" 16岁是少年!");break;
case 17: System.out.println(" 17岁是少年!");break;
case 18: System.out.println(" 18岁是青年!");break;
case 19: System.out.println(" 19岁是青年!");break;
case 20: System.out.println(" 20岁是青年!");break;
case 21: System.out.println(" 21岁是青年!");break;
case 22: System.out.println(" 22岁是青年!");break;
case 23: System.out.println(" 23岁是青年!");break;
case 24: System.out.println(" 24岁是青年!");break;
case 25: System.out.println(" 25岁是青年!");break;
case 26: System.out.println(" 26岁是青年!");break;
case 27: System.out.println(" 27岁是青年!");break;
case 28: System.out.println(" 28岁是青年!");break;
case 29: System.out.println(" 29岁是青年!");break;
case 30: System.out.println(" 30岁是中年!");break;
case 31: System.out.println(" 31岁是中年!");break;
case 32: System.out.println(" 32岁是中年!");break;
case 33: System.out.println(" 33岁是中年!");break;
case 34: System.out.println(" 34岁是中年!");break;
case 35: System.out.println(" 35岁是中年!");break;
case 36: System.out.println(" 36岁是中年!");break;
case 37: System.out.println(" 37岁是中年!");break;
case 38: System.out.println(" 38岁是中年!");break;
case 39: System.out.println(" 39岁是中年!");break;
case 40: System.out.println(" 40岁是中年!");break;
case 41: System.out.println(" 41岁是中年!");break;
case 42: System.out.println(" 42岁是中年!");break;
case 43: System.out.println(" 43岁是中年!");break;
case 44: System.out.println(" 44岁是中年!");break;
case 45: System.out.println(" 45岁是中年!");break;
case 46: System.out.println(" 46岁是中年!");break;
case 47: System.out.println(" 47岁是中年!");break;
case 48: System.out.println(" 48岁是中年!");break;
case 49: System.out.println(" 49岁是中年!");break;
case 50: System.out.println(" 50岁是中年!");break;
case 51: System.out.println(" 51岁是中年!");break;
case 52: System.out.println(" 52岁是中年!");break;
case 53: System.out.println(" 53岁是中年!");break;
case 54: System.out.println(" 54岁是中年!");break;
case 55: System.out.println(" 55岁是中年!");break;
case 56: System.out.println(" 56岁是中年!");break;
case 57: System.out.println(" 57岁是中年!");break;
case 58: System.out.println(" 58岁是中年!");break;
case 59: System.out.println(" 59岁是中年!");break;
case 60: System.out.println(" 60岁是老年!");break;
case 61: System.out.println(" 61岁是老年!");break;
case 62: System.out.println(" 62岁是老年!");break;
case 63: System.out.println(" 63岁是老年!");break;
case 64: System.out.println(" 64岁是老年!");break;
case 65: System.out.println(" 65岁是老年!");break;
case 66: System.out.println(" 66岁是老年!");break;
case 67: System.out.println(" 67岁是老年!");break;
case 68: System.out.println(" 68岁是老年!");break;
case 69: System.out.println(" 69岁是老年!");break;
case 70: System.out.println(" 70岁是老年!");break;
case 71: System.out.println(" 71岁是老年!");break;
case 72: System.out.println(" 72岁是老年!");break;
case 73: System.out.println(" 73岁是老年!");break;
case 74: System.out.println(" 74岁是老年!");break;
case 75: System.out.println(" 75岁是老年!");break;
case 76: System.out.println(" 76岁是老年!");break;
case 77: System.out.println(" 77岁是老年!");break;
case 78: System.out.println(" 78岁是老年!");break;
case 79: System.out.println(" 79岁是老年!");break;
case 80: System.out.println(" 80岁是老年!");break;
case 81: System.out.println(" 81岁是老年!");break;
case 82: System.out.println(" 82岁是老年!");break;
case 83: System.out.println(" 83岁是老年!");break;
case 84: System.out.println(" 84岁是老年!");break;
case 85: System.out.println(" 85岁是老年!");break;
case 86: System.out.println(" 86岁是老年!");break;
case 87: System.out.println(" 87岁是老年!");break;
case 88: System.out.println(" 88岁是老年!");break;
case 89: System.out.println(" 89岁是老年!");break;
case 90: System.out.println(" 90岁是老年!");break;
case 91: System.out.println(" 91岁是老年!");break;
case 92: System.out.println(" 92岁是老年!");break;
case 93: System.out.println(" 93岁是老年!");break;
case 94: System.out.println(" 94岁是老年!");break;
case 95: System.out.println(" 95岁是老年!");break;
case 96: System.out.println(" 96岁是老年!");break;
case 97: System.out.println(" 97岁是老年!");break;
case 98: System.out.println(" 98岁是老年!");break;
case 99: System.out.println(" 99岁是老年!");break;
case 100: System.out.println("100岁是老年!");break;
default: System.out.println("你确认你输入正确?!");break;
}
}

以上代码非手工输入,由下面的clojure代码生成:
[code=Clojure](defn emitter [b e x]
(doseq [age (range b e)]
(println (format " case %3d: System.out.println(\"%3d岁是%s!\");break;" age age x))))
(println "public void testAge(int age){")
(println " switch(age){")
(emitter 0 18 "少年")
(emitter 18 30 "青年")
(emitter 30 60 "中年")
(emitter 60 101 "老年")
(println " default: System.out.println(\"你确认你输入正确?!\");break;")
(println " }")
(println "}")
[/code]
回复
csofwood 2010-09-01
晕,没人回答,是太简单了吗?
回复
相关推荐
发帖
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2010-08-29 10:00
社区公告
暂无公告