Exception in thread "main" java.lang.NullPointerException
at javaapplication67.arithmcalc.main(arithmcalc.java:24)
distanceFromLocation
答案 0 :(得分:1)
您可以通过删除第10行和第12行并修改下面给出的代码来避免错误。
import java.util.Scanner;
public class arithmcalc {
public static void main(String[] args) {
int operand1;
int operand2;
char operator;
Scanner sc = new Scanner(System.in);
System.out.println("enter operan1 operand2 and operator one by one");
operand1 = sc.nextInt();
operand2 = sc.nextInt();
operator = sc.next().charAt(0);
int result = 0;
switch (operator) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
default:
System.out.println("illegal operand");
}
System.out.println("Result : " + result);
}
}
Hoper这很有帮助。