论坛首页 Java企业应用论坛

奇怪的 switch() case 题目

浏览 16570 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2009-03-19  
大概又是啊个学校,哪个变态老师出的题目吧?吃饱了撑的
0 请登录后投票
   发表时间:2009-03-19  
只用switch、case不用其他的?:是根本无法做出来的
0 请登录后投票
   发表时间:2009-03-19   最后修改:2009-03-19
         switch (x)  
         {  
             case x<1:  
             System.out.println("x is less than 1");  
             break;  
               
             case x==1: 
             System.out.println("x is 1");  
             break;  
               
             case x<=4:  
             System.out.println("x is 2,3,4");  
             break;  
               
             default:  
             System.out.println("x is greater than 4");  
             break;  
     }  



这样不行么?干嘛呀case那么多啊?
0 请登录后投票
   发表时间:2009-03-19  
duduli 写道
public static void testCase(int x) {
switch (x) {
case 1:
System.out.println("is 1");
break;
case 2:
case 3:
case 4:
System.out.println("is 2 3 4");
break;
default:
x = x>4?5:-1;
case 5:
System.out.println("big 4");
break;
case -1:
System.out.println("less 1");
}
}

Head First设计模式■ 群共同努力

Ta的运行不正确

正确运行:
public class SpecialSwitch {
public static void main(String[] args){
int data[]=new int[]{5};
int len=data.length;
for(int i=0;i<len;i++){
testCase(data[i]);
}
}
public static void testCase(int x) {
int data=x;
System.out.println(data);
switch (data) {
case 1:
System.out.println("is 1");
break;
case 2:
case 3:
case 4:
System.out.println("is 2 3 4");
break;
default:
data = data>4?5:-1;
case -1:
System.out.println("less 1");
break;
case 5:
System.out.println("big 4");
break;
}
}
}
0 请登录后投票
   发表时间:2009-03-19  
duduli 写道
public static void testCase(int x) {
switch (x) {
case 1:
System.out.println("is 1");
break;
case 2:
case 3:
case 4:
System.out.println("is 2 3 4");
break;
default:
x = x>4?5:-1;
case 5:
System.out.println("big 4");
break;
case -1:
System.out.println("less 1");
}
}

Head First设计模式■ 群共同努力

-1和5需要调换位置,否则结果不正确
0 请登录后投票
   发表时间:2009-03-19  
ninecat 写道
都不对,题目很明显只让用switch 而不能用其他的判断比如if\3元表达式(for 和while应该也不算)
看看这个怎么样:
public class Switch {
	public static void main(String[] args) {
		
		int x =5;
		try {	
			switch ((x-1)/x) {
			case 1:
			case 2:	
				System.out.println("x is less than 1");
				break;
			default:
				switch ((x+4)/x) {
					case 5:
						System.out.println("x is 1");
						break;
					case 3:
					case 2:
						System.out.println("x is 2,3,4");
						break;
					case 1:
						System.out.println("x is greater than 4");
						break;
				}
				break;
			}
		} catch (Exception e) {
			System.out.println("x is less than 1");
		}
	}
}

异常都用上了呵呵呵
0 请登录后投票
   发表时间:2009-03-20  
gufenglian 写道
凑凑热闹,我这样写的
switch(x>4?5:(x<1?0:x)){
case 0:System.out.println("x小于1"); break;
case 1:System.out.println("x=1"); break;
case 2:
case 3:
case 4:System.out.println("x是2,3,4"); break;
case 5:System.out.println("x大于4"); break;
}

我喜欢这种风格
0 请登录后投票
   发表时间:2009-03-20  
it.go 写道
gufenglian 写道
凑凑热闹,我这样写的
switch(x>4?5:(x<1?0:x)){
case 0:System.out.println("x小于1"); break;
case 1:System.out.println("x=1"); break;
case 2:
case 3:
case 4:System.out.println("x是2,3,4"); break;
case 5:System.out.println("x大于4"); break;
}

我喜欢这种风格


我不光喜欢风格 。 ^^
0 请登录后投票
   发表时间:2009-03-23  
好无聊的题目,用个if会死啊?
0 请登录后投票
   发表时间:2009-03-30  
寻梦之龙 写道
duduli 写道
public static void testCase(int x) {
switch (x) {
case 1:
System.out.println("is 1");
break;
case 2:
case 3:
case 4:
System.out.println("is 2 3 4");
break;
default:
x = x>4?5:-1;
case 5:
System.out.println("big 4");
break;
case -1:
System.out.println("less 1");
}
}

Head First设计模式■ 群共同努力

-1和5需要调换位置,否则结果不正确


哎,你们怎么都不测试一下的,换了位置还不是一样不正确!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics