编程语言
首页 > 编程语言> > java用switch写简易计算器

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,scanner,System,break,switch,计算器,println,ys,out
来源: https://www.cnblogs.com/itszx/p/15611858.html