java用switch写简易计算器

import java.util.Scanner;

public class test04 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);

double x ,y,s;
x=0.0;
y=0.0;
s=0.0;
char ys;

System.out.println("请输入第一个数:");
x = scanner.nextDouble();
System.out.println("请输入第二个数:");
y = scanner.nextDouble();
System.out.println("请输入运算符:");;
ys = scanner.next().charAt(0);
System.out.println(ys);
switch (ys) {
case '+':
s=x+y;

break;
case '-':
s=x-y;

break;
case '*':
s=x*y;

break;


case '/':
if(y==0){
System.out.println("除数不能为0");
return;
}
s=x/y;

break;
default:
System.out.println("运算符错误!");
return;
}
System.out.println(""+x+ys+y+"="+s);
}

}

java用switch写简易计算器

 

 

 

上一篇:转义字符与原字符


下一篇:Delphi - 采用第三方控件TMS、SPComm开发串口调试助手