首页 > 代码库 > java四则运算写法

java四则运算写法

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四则运算写法