所以这是一个计算器问题。我们要有一个Calculator类(包含其他三个类),Number类(用于类型检查要处理的数量),Operation类(用于五个操作))和Memory类(用于存储器函数)) 。
当我输入数字时,它不会输出数字,也不会计算输入的数字。对不起,如果有任何混淆。任何帮助将不胜感激。
import java.util.*;
public class calculatortest{
private Num num;
private Ope ope;
public static void main(String args[]){
calculatortest c = new calculatortest();
c.start();
}
public calculatortest()
{
}
public void start()
{
describeProgram();
getInput();
displayOutput();
//getMemory();
}
private void describeProgram()
{
System.out.println("this is kc program and it starts from here");
}
private void displayOutput()
{
System.out.println("Input number 1: " + num.number1());
System.out.println("Input operator: " + ope.operator());
System.out.println("Input number 2: " + num.number2());
System.out.println("Answer is: " + ope.answer());
}
private void getInput()
{
double number11;
String operator2;
double number22;
Scanner scanner = new Scanner(System.in);
System.out.print("Number 1: ");
number11 = scanner.nextDouble();
System.out.print("Operator: ");
operator2 = scanner.next();
System.out.print("Number 2: ");
number22 = scanner.nextDouble();
//operation = new Operation(number11,operator2, number33);
num = new Num (number11, number22);
ope = new Ope (number11, number22);
}
}
class Num{
private double number11;
private double number22;
public Num (double number11, double number22)
{
}
public double number1()
{
return number11;
}
public double number2()
{
return number22;
}
}
class Ope
{
String operator2;
double answer;
double num1, num2;
public Ope(double number11, double number22 )
{
setNumber11(number11);
setNumber22(number22);
}
public String operator() {
// TODO Auto-generated method stub
return ("does not work");
}
/*public String Ope(String operator)
{
return operator2;
}*/
public void setNumber11(double number11)
{
num1 = number11;
}
public void setNumber22(double number22)
{
num2 = number22;
}
public double answer() {
if(operator2.equals("+"))
{
double answer = num1 + num2;
return answer;
}
else if(operator2.equals("-"))
{
double answer = num1 - num2;
return answer;
}
else if(operator2.equals("*"))
{
double answer = num1 * num2;
return answer;
}
else if(operator2.equals("/"))
{
double answer = num1 / num2;
return answer;
}
return 0;
}
}
//class Mem
This is the OUTPUT:
this is kc program and it starts from here
Number 1: 5
Operator: +
Number 2: 5
Exception in thread "main" java.lang.NullPointerException
at Ope.answer(calculatortest.java:106)
at calculatortest.displayOutput(calculatortest.java:33)
at calculatortest.start(calculatortest.java:20)
at calculatortest.main(calculatortest.java:9)
Input number 1: 0.0
Input operator: does not work
Input number 2: 0.0
答案 0 :(得分:-1)
您应该将构造函数定义为接受这样的运算符:
public Ope(double number11, String operator, double number22 )
{
operator2 = operator;
setNumber11(number11);
setNumber22(number22);
}
然后在getInput上执行:
operation = new Ope(number11,operator2,number33);