public class Addition extends Calculator implements ICalculator{
public double getResult()
{
double result=0;
result=number_A+number_B;
return result;
}
}
为什么到除法类的时候会报错,不解?
public class Division extends Calculator implements ICalculator {
public double getResult()
{
double result=0;
result=number_A/number_B;
return result;
}
}
number_A number_B一直是红线,不解??
------最佳解决方案--------------------
我现在也可以了。。不过新建了一个TestCalculator类为主方法类来测试一下。
不知怎样写。
import java.util.Scanner;
public class TestCalculator {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculator cal = null;
ICalculator calc = null;
Scanner rd=new Scanner(System.in);
System.out.println("Please enter the first number:");
cal.setNumberA(rd.nextDouble());
System.out.println("Please enter the second number:");
cal.setNumberB(rd.nextDouble());
System.out.println("Please select the operator symbol:");
System.out.println("1.+ 2.- 3.* 4./");
int sign=rd.nextInt();
switch(sign)
{
case 1:
cal=new Addition();
break;
case 2:
cal=new Subtraction();
break;
case 3:
cal=new Multiplication();
break;
}
System.out.println(cal.number_A+""+sign+""+cal.number_B+"="+calc.getResult());
}
------其他解决方案--------------------
保存了。。郁闷了,我以为是子类不能这样用父类的属性,哎 ------其他解决方案-------------------- 做除法运算时,必须要为int型数据,java中好像是这样用的 ------其他解决方案-------------------- 没看明白啊。 ------其他解决方案-------------------- public double number_B=0;