设x=10,y=5, 试用if-else结构判断输入值是否为字符‘+’,switch结构实现当输入字符‘+’时,计算x,y的和

来源:百度知道 编辑:UC知道 时间:2024/05/25 06:16:31
JAVA程序题 恳请大侠帮忙 不胜感激
输入值是有‘+’、‘-’、‘*’、‘/’四个,要计算的是和、差、积、商。但题目太长写不下,就省略了一下

int x = 10, y = 5;
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
char c = s.charAt(0);
switch (c) {
case '+':
System.out.println(x + y);
break;
case '-' :
System.out.println(x - y);
break;
case '*' :
System.out.println(x * y);
break;
case '/' :
System.out.println(x / y);
break;
default:
System.out.println("Invalid Input");
}

差不多就是这样。。当然这个程序并不健壮。。
还是不明白为什么同时需要switch和if-else。。。

你是用什么语言啊!没有语言的规定怎么写。而且switch语言中不允许出现字符比较的!除非你定义的宏或者const常量