论坛首页 Java企业应用论坛

奇怪的 switch() case 题目

浏览 16569 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (1)
作者 正文
   发表时间:2009-03-03   最后修改:2009-03-03
OO
给出一值X,用Case判断其值:
X小于1
X等于1
X是2,3,4
X大于4

类似这段代码:
public class siwtch
{
    public static void main(String[] args)
    {
        int x = -2;
        
        switch (x)
        {
            //case int x<1:
            case 1:
            System.out.println("x is less than 1");
            break;
            
            //case x==1:
            case 2:
            System.out.println("x is 1");
            break;
            
            //case x<=4:
            case 3:
            System.out.println("x is 2,3,4");
            break;
            
            case 4:
            System.out.println("x is greater than 4");
            break;
    }
        
}
}


怎样写的这个case,才能有正确的输出?
-1 输出 X小于1
1  输出 X等于1
2  输出 X是2,3,4
5  输出 X大于4

   发表时间:2009-03-03  
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设计模式■ 群共同努力
0 请登录后投票
   发表时间:2009-03-03  
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&gt;4?5:-1;
case 5:
System.out.println("big 4");
break;
case -1:
System.out.println("less 1");
}
}

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

不错!
0 请登录后投票
   发表时间:2009-03-03  
原来default可以这样用的,duduli牛呀
0 请登录后投票
   发表时间:2009-03-03   最后修改:2009-03-03
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设计模式■ 群共同努力

如果按你的CODE,if X > 4 or < -1 ,则结果都是 big 4

               default:
x = x > 4 ? 5 : -1;
switch (x) {
case 5:
System.out.println("x is 5");
break;
case -1:
System.out.println("x is less than 1");
break;
}
0 请登录后投票
   发表时间:2009-03-03  
xiespirit 写道
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设计模式■ 群共同努力

如果按你的CODE,if X > 4 or < -1 ,则结果都是 big 4

               default:
x = x > 4 ? 5 : -1;
switch (x) {
case 5:
System.out.println("x is 5");
break;
case -1:
System.out.println("x is less than 1");
break;
}

对 你试试x=-2 也是大于4 漏洞啊!
0 请登录后投票
   发表时间:2009-03-03  
不要太容易相信别人 首先应该相信自己
0 请登录后投票
   发表时间:2009-03-03  
这样吧,兄弟!          

                  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;
switch (x){
case 5:
System.out.println("big 4");
break;
default:System.out.println("less 1");
}
}
0 请登录后投票
   发表时间:2009-03-03  
ych19850810 写道

这样吧,兄弟!&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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&gt;4)?5:-1;
switch (x){
case 5:
System.out.println("big 4");
break;
default:System.out.println("less 1");
}
}

我测试了,这样正确,第一次就是因为没有嵌套!
0 请登录后投票
   发表时间:2009-03-13  
需要那么复杂吗?我这样就行啊!

        int x = 5;  
        switch (x) {   
            case 1:  
            System.out.println("x is 1");  
            break;         
            case 2:
            case 3: 
            case 4: 
            System.out.println("x is 2,3,4");  
            break;               
            case 5:  
            System.out.println("x is greater than 4");
            break;  
            default:
            System.out.println("x is less than 1");  
        }
1 请登录后投票
论坛首页 Java企业应用版

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