`
luzl
  • 浏览: 575496 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

奇怪的 switch() case 题目

    博客分类:
  • Java
阅读更多
给出一值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

分享到:
评论
34 楼 huashengniuga 2009-09-16  
我的处女帖就给了你吧!
以下是我的除了switch case以外没有其他判断的实现。

public class Switch {
	public static void main(String[] args) {
		for (int x = -10; x < 10; x++) {
			System.out.print(x);
			switch (x) {
			case 1:
				System.out.println(" is 1");
				break;
			case 2:
			case 3:
			case 4:
				System.out.println(" is 2,3 or 4");
				break;
			default:
				switch (-x % (x - 1)) {
				case -1:
					System.out.println(" is greater than 4");
					break;
				default:
					System.out.println(" is less than 1");
					break;
				}
				break;
			}
		}
	}
}


结果
-10 is less than 1
-9 is less than 1
-8 is less than 1
-7 is less than 1
-6 is less than 1
-5 is less than 1
-4 is less than 1
-3 is less than 1
-2 is less than 1
-1 is less than 1
0 is less than 1
1 is 1
2 is 2,3 or 4
3 is 2,3 or 4
4 is 2,3 or 4
5 is greater than 4
6 is greater than 4
7 is greater than 4
8 is greater than 4
9 is greater than 4
33 楼 satanest 2009-09-15  
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");
		}
	}
}

魔幻数字。。。。
32 楼 Hooopo 2009-09-15  
纯路过。。。

def bt_case(x)
  infinity = -1/0.0
  case x
  when (infinity...1) then puts "less then 1"
  when 1              then puts "x is 1"
  when (2..4)         then puts "one of 2 3 4"
  when (5..-infinity) then puts " bigger then 4"
  end
end


结果:

irb(main):065:0> bt_case(1)
x is 1
=> nil
irb(main):066:0> bt_case(0)
less then 1
=> nil
irb(main):067:0> bt_case(-1)
less then 1
=> nil
irb(main):068:0> bt_case(2)
one of 2 3 4
=> nil
irb(main):069:0> bt_case(3)
one of 2 3 4
=> nil
irb(main):070:0> bt_case(5)
 bigger then 4
=> nil
irb(main):071:0> bt_case(8)
 bigger then 4
=> nil
irb(main):072:0> bt_case(8.5)
 bigger then 4
=> nil
irb(main):073:0>


















31 楼 case0079 2009-04-02  
建立一个公式吧

Y=X<1:0?(X<5?X:5)

这样不知道可以不?没用过嵌套三元表达式
30 楼 hunter4java 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设计模式■ 群共同努力

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;
}
}
}



没理解switch的用法,就乱写,换了位置和没换都一样有问题啊。不相信,你测试下6,结果是less 1

default:
data = data>4?5:-1;
case -1:
System.out.println("less 1");
break;
case 5:
System.out.println("big 4");
break;

这个其实了解一下switch语句用if else来实现就知道了。
始终是判断data的,如果找不到,就按default处理,如果default没有break跳出语句,那么他后面的语句将依次执行。
所以如果data的值不是{1,2,3,4,5,-1}中一个的话,都会执行接下来的case语句,打印less 1。
然后break跳出,所以case 5语句始终是没有执行到的。
你换个位置,只不过是把case 5始终执行,case -1 永远不执行罢了。

其实和
switch(1){
  case 1 : System.out.println(1);
  case 2 : System.out.println(2);
           break;
  }
打印 1
      2
一样的道理。
29 楼 hunter4java 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需要调换位置,否则结果不正确


哎,你们怎么都不测试一下的,换了位置还不是一样不正确!
28 楼 groovyzhou 2009-03-23  
好无聊的题目,用个if会死啊?
27 楼 男儿当 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;
}

我喜欢这种风格


我不光喜欢风格 。 ^^
26 楼 it.go 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;
}

我喜欢这种风格
25 楼 surpass 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");
		}
	}
}

异常都用上了呵呵呵
24 楼 寻梦之龙 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需要调换位置,否则结果不正确
23 楼 寻梦之龙 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;
}
}
}
22 楼 haitwin 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那么多啊?
21 楼 vlinux 2009-03-19  
只用switch、case不用其他的?:是根本无法做出来的
20 楼 nowaytj 2009-03-19  
大概又是啊个学校,哪个变态老师出的题目吧?吃饱了撑的
19 楼 cenxiaobai 2009-03-19  
应该考虑0的吧
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 0:
case -1:
System.out.println("less 1");
}
}
18 楼 dahui12344321 2009-03-19  
<div class="quote_title">bravewu 写道</div>
<div class="quote_div">需要那么复杂吗?我这样就行啊!
<br />
<br />        int x = 5;  
<br />        switch (x) {   
<br />            case 1:  
<br />            System.out.println("x is 1");  
<br />            break;         
<br />            case 2:
<br />            case 3: 
<br />            case 4: 
<br />            System.out.println("x is 2,3,4");  
<br />            break;               
<br />            case 5:  
<br />            System.out.println("x is greater than 4");
<br />            break;  
<br />            default:
<br />            System.out.println("x is less than 1");  
<br />        } </div>
<p> </p>
<p> </p>
17 楼 yucc77 2009-03-17  
不要为了简洁而搞得晦涩难懂,不值得
16 楼 gaohuier 2009-03-16  
int x = 5;
int y = x < 1 ? -1 : x == 1 ? 1 : x == 2 || x == 3 || x == 4 ? 2
: x > 4 ? 5 : 0;
switch (y) {
case 0:
System.out.println("非法数字........");
break;
// case int x<1:
case -1:
System.out.println("x is less than 1");
break;

// case x==1:
case 1:
System.out.println("x is 1");
break;

// case x<=4:
case 2:
System.out.println("x is 2,3,4");
break;

case 5:
System.out.println("x is greater than 4");
break;
}
15 楼 ninecat 2009-03-16  
都不对,题目很明显只让用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");
		}
	}
}

相关推荐

    Python-switchcase用纯Python实现的SwitchCase结构

    标题提到的"Python-switchcase"是一个项目,旨在通过纯Python代码实现一个类似于`switch-case`的功能。下面将详细介绍如何实现以及使用这样的结构。 在Python中,`switch-case`的常见实现方法是使用字典...

    PyPI 官网下载 | switchcase-1.0.tar.gz

    《PyPI官网下载 | switchcase-1.0.tar.gz - Python库详解》 在Python编程中,我们常常需要根据不同的条件执行不同的代码块,这在其他一些编程语言中可以通过switch-case语句实现。然而,Python并没有内置的switch-...

    用函数指针替代Switch/Case语句的程序设计方法

    单片机程序中,当Switch/Case语句分支较多、处理代码较长、处理情况较为复杂时,逻辑修改和程序调试均存在一定的困难。针对该问题,本文给出了使用函数指针替代Switch/Case语句的实现思路以及相对应的代码模型,为...

    switch+case语句例子成绩

    `switch-case` 语句是 Java 语言中的一个控制流结构,它允许程序根据不同的条件分支执行不同的代码块。在给定的例子中,`switch-case` 语句被用来根据学生的成绩分配相应的等级,如 A、B、C、D 或 F。 首先,我们...

    switch case.docx

    Java 语言中的 `switch case` 语句是一种流程控制结构,用于执行多个条件分支中的一个。这个语句常用于简化基于不同条件执行不同操作的代码。在给定的例子中,`switch case` 被用来根据学生成绩等级输出相应的评价。...

    switch……case练习

    本文档为switch……case的练习代码,里面包含之前的所有代码

    Arduino项目开发 Control_switchCase2_switchCase2.pdf

    在给定的文件 `Control_switchCase2_switchCase2.pdf` 中,示例代码展示了如何使用 `switch` 语句处理串行输入,并通过控制 LED 的亮灭来响应不同字符。以下是对这个知识点的详细解释: 首先,`switch` 语句通常...

    Java switch case 语句.docx

    Java的switch case语句是一种控制流程结构,用于根据变量的值执行不同的代码块。它提供了一种更简洁的方式来替代多个if...else if...else语句。以下是对switch case语句的详细解释: 1. **基本语法**: switch语句...

    switch_java_switch-case_例题_

    switch case 两个例题。1.对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。2.从键盘上输入2019年的“month”和“day”,要求通过程序输出输入的日期为2019年的第几天。

    Arduino项目开发 Control_switchCase_switchCase.pdf

    在 Arduino 项目开发中,`switch-case` 语句是一个重要的控制结构,它允许程序根据变量的不同值执行不同的代码块。这个例子展示了如何在 Arduino 上使用 `switch-case` 结构来处理模拟输入(如光电传感器)的数据,...

    switch case和循环结构.md

    自己上课记得一些笔记,记录一下上课进程,小白入门可以看 ,目前更新到分支结构,后期定期更新一些新的内容

    14.2 用字典映像代替switch case语句|Pythonic与Python杂记|Python3.8入门 & 进阶 & 原生爬虫实战完全解读

    14.2_用字典映射代替switch_case语句|Pythonic与Python杂记|Python3.8入门_&_进阶_&_原

    js中switch case循环实例代码.docx

    JS 中 switch case 循环实例代码详解 在 JavaScript 中,switch 语句是一种常用的控制流语句,用于根据不同的条件执行不同的操作。下面我们将深入探讨 JS 中 switch case 循环实例代码的实现细节。 一、switch ...

    基于C语言实现switch case语句(源码)

    使用switch case语句根据不同的成绩等级进行匹配和输出相应的评价。在这里,使用了case语句的多值匹配,例如'A'和'a'都会输出"Excellent!"。 如果用户输入的成绩等级不在'A'、'B'、'C'、'D'、'F'范围内,则执行...

    Python 实现switch case功能

    Python 实现switch case功能 在 Python 中,没有内置的 switch-case 语句,但你可以使用字典和函数来模拟它。 在这个示例中,switch_case 函数接受一个参数 argument,根据传入的参数值,从字典 switcher 中获取相应...

    switch case语句例子成绩.md

    switch case语句例子成绩

    c#中switch case的用法实例解析

    在本实例中,我们看到`switch`与`case`结合使用,来实现一个简单的收银付费小程序。下面我们将深入探讨`switch`和`case`的用法,并基于提供的代码片段进行详细解释。 首先,`switch`语句通常包含一个表达式,该...

    python中Switch/Case实现的示例代码

    学习Python过程中,发现没有switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现。所以不妨自己来实现Switch/Case功能。 使用if…elif…elif…else 实现switch/case 可以使用if…elif…elif.....

    c语言switch case语句.docx 代码

    C语言中的`switch`和`case`语句是控制流程的一部分,它们提供了多路选择的结构,使得程序可以根据不同的条件执行不同的代码块。在C语言中,`switch`语句通常用于替代一系列的`if...else if...else`语句,以实现更...

Global site tag (gtag.js) - Google Analytics