选择运营商的计划

时间:2015-03-14 17:01:27

标签: java string char operators

    import java.util.*;
class calc123
{
    public static void main()
    {
        int a,b,c;
        char a1;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter first number");
        a=sc.nextInt();
        System.out.println("Enter second number");
        b=sc.nextInt();
        System.out.println("Enter the operator");
        a1=sc.next().charAt(0);
        if(a1="+")
        System.out.println(a+b);
        else if(a1="-")
        System.out.println(a-b);
        else if(a1="/")
        System.out.println(a/b);
        else if(a1="*")
        System.out.println(a*b);
        else if(a1="%")
        System.out.println(a%b);
        else
        System.out.println("Invalid Operator");
    }
}

面临的问题是(a1 =" +")

incompatible types: java.lang.String cannot be converted to char. 

1 个答案:

答案 0 :(得分:0)

(a1 =“+”)不会返回true或false,它将返回“+”!,请提高您对Java的基本知识。另请注意,a1是char类型,而“+”是String。