package exercise;
public class Calaculator {
String choice="";
double num1;
double num2;
public double cala(double num1,double num2){
double ret=0;
if (choice.equals("-")) {
ret=num1-num2;
}
else if (choice.equals("+")) {
ret=num1+num2;
}
else if (choice.equals("*")) {
ret=num1*num2;
}
else if (choice.equals("/")) {
ret=num1/num2;
}
return ret;
}
}
测试程序
package exercise;
import java.util.Scanner;
public class TxstCala {
public static void main(String[] args) {
double num1=0;
double num2=0;
Scanner input=new Scanner(System.in);
Calaculator calaculator=new Calaculator();
System.out.println("请输入第一个数");
calaculator. num1=input.nextDouble();
System.out.println("请输入运算符号");
calaculator.choice=input.next();
System.out.println("请输入第二个数");
calaculator.num2=input.nextDouble();
double ret=calaculator.cala(num1, num2);
System.out.println(ret);
}
}
public class Calaculator {
String choice="";
double num1;
double num2;
public double cala(double num1,double num2){
double ret=0;
if (choice.equals("-")) {
ret=num1-num2;
}
else if (choice.equals("+")) {
ret=num1+num2;
}
else if (choice.equals("*")) {
ret=num1*num2;
}
else if (choice.equals("/")) {
ret=num1/num2;
}
return ret;
}
}
测试程序
package exercise;
import java.util.Scanner;
public class TxstCala {
public static void main(String[] args) {
double num1=0;
double num2=0;
Scanner input=new Scanner(System.in);
Calaculator calaculator=new Calaculator();
System.out.println("请输入第一个数");
calaculator. num1=input.nextDouble();
System.out.println("请输入运算符号");
calaculator.choice=input.next();
System.out.println("请输入第二个数");
calaculator.num2=input.nextDouble();
double ret=calaculator.cala(num1, num2);
System.out.println(ret);
}
}