Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 选择操作员的程序_Java_String_Char_Operators - Fatal编程技术网

Java 选择操作员的程序

Java 选择操作员的程序,java,string,char,operators,Java,String,Char,Operators,它面临的问题是(a1=“+”)是 (a1=“+”)不会返回true或false,它将返回“+”!,请提高您对Java的基本知识。还要注意a1是字符类型,而“+”是字符串。仍然无法执行程序Sotirios Delimanolis刚刚编辑过问题。没有人提供编辑过的问题的解决方案。也请检查一下,我应该使用什么来代替“+”使用a1='+',如果这里有其他选项,您也可以使用switch case运算符比较两个操作数的值,而=运算符为左侧操作数赋值并返回相同的值!但我只有在其他情况下才能使用 imp

它面临的问题是(a1=“+”)是


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

仍然无法执行程序Sotirios Delimanolis刚刚编辑过问题。没有人提供编辑过的问题的解决方案。也请检查一下,我应该使用什么来代替“+”使用a1='+',如果这里有其他选项,您也可以使用switch case运算符比较两个操作数的值,而=运算符为左侧操作数赋值并返回相同的值!但我只有在其他情况下才能使用
    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");
    }
}
incompatible types: java.lang.String cannot be converted to char.