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