You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
710 B
31 lines
710 B
package calculator;
|
|
|
|
import java.util.Scanner;
|
|
|
|
public class cal4 {
|
|
public cal4() {
|
|
Scanner inScanner=new Scanner(System.in);
|
|
System.out.print("请输入第一位运算数:");
|
|
double a=inScanner.nextInt();
|
|
System.out.print("请输入第二位运算数:");
|
|
double b=inScanner.nextInt();
|
|
System.out.print("请输入将要运算的运算符:");
|
|
String op=inScanner.next();
|
|
char ch=op.charAt(0);
|
|
double sum=cala(a, b, ch);
|
|
System.out.println("结果为:"+sum);
|
|
}
|
|
|
|
public static double cala(double x,double y,char op) {
|
|
switch (op) {
|
|
case '+': return x+y;
|
|
case '-': return x-y;
|
|
case '*': return x*y;
|
|
case '/':
|
|
if(y==0) return 0;
|
|
else return x/y;
|
|
default:
|
|
return x;
|
|
}
|
|
}
|
|
} |