码迷,mamicode.com
首页 > 编程语言 > 详细

java四则运算写法

时间:2017-05-14 13:57:22      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:default   public   四则运算   int()   out   oid   ext   第一个   cas   

public class 四则运算{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("请输入第一个数字:");
int a = sc.nextInt();
System.out.print("请输入运算字符:");
String str = sc.next();
char ch = str.charAt(0);
System.out.print("请输入第二个数字:");
int b = sc.nextInt();
switch(ch)
{
case ‘+‘:
System.out.println(a+"+"+ b + "="+(a+b));
break;
case ‘-‘:
System.out.println(a+"-"+ b+ "="+(a-b));
break;
case ‘*‘:
System.out.println(a+"*"+ b+ "="+(a*b));
break;
case ‘/‘:
if(b==0){
System.out.println("被除数为零,运算无意义!");
break;
}
else {
System.out.println(a+"/"+ b+ " = "+(a/b));
break;
}
default:
System.out.println("运算符是无意义字符!");
}
}

java四则运算写法

标签:default   public   四则运算   int()   out   oid   ext   第一个   cas   

原文地址:http://www.cnblogs.com/huoxiansudi/p/6852161.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!