java吧 关注:1,234,860贴子:12,702,077
  • 1回复贴,共1

这哪里错了

只看楼主收藏回复

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);
}
}


1楼2015-06-04 17:38回复
    简单 计算器的实现+ - * / 的


    2楼2015-06-04 17:39
    回复