通过扫描程序在java中使用operator

时间:2016-08-23 08:26:25

标签: java

Exception in thread "main" java.lang.NullPointerException
at javaapplication67.arithmcalc.main(arithmcalc.java:24)
distanceFromLocation

1 个答案:

答案 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这很有帮助。