我在运算符标志中的计算器代码中有问题

时间:2019-06-20 01:16:35

标签: java

当我输入2个数字并且运算符显示无效的运算符.. directly直接在else语句上输入时,它必须在运算符上输入+,-,*,/,%,我想在输入+运算符时将两个数字加起来

import java.util.Scanner; 公共类苹果{

private static int num1;
private static int num2;

public static int Sum(int num1, int num2){
return num1 + num2; 
}
public static int Sub(int num1, int num2){
    return num1 - num2;
}
public static int Multi(int num1, int num2){
    return num1 * num2;
}
public static int Div(int num1, int num2){
    return num1 / num2;
}
public static int Mod(int num1, int num2){
    return num1 % num2;
}

public static void main(String[]args){
    Scanner input = new Scanner(System.in);

    double fnum, snum;
    String operation;

    System.out.print("Enter First Number: ");
    fnum = input.nextDouble();

    System.out.print("Enter operation(+,-,*,/,%): ");
    operation= input.next();

    System.out.print("Enter Second Number: ");
    snum = input.nextDouble();

    if(operation == "+"){
        System.out.println(fnum + "+" + snum + "=" + Sum(num1, num2));
    }
    else if(operation == "-"){
        System.out.println(fnum + "-" + snum + "=" + Sub(num1,num2));
    }
    else if(operation == "*"){
        System.out.println(fnum + "*" + snum + "=" + Multi(num1,num2));
    }
    else if(operation == "/"){
        System.out.println(fnum + "/" + snum + "=" + Div(num1,num2));
    }
    else if(operation == "%"){
        System.out.println(fnum + "%" + snum + "=" + Mod(num1,num2));
    }
    else{
        System.out.println("Invalid operation!");
    }

    }
}

0 个答案:

没有答案