发现不兼容的类型:java.lang.String
incompatible types found : java.lang.String
我正在学习 java 并且我编写了这个程序,它在我大学的计算机上编译成功但在我家里编译不成功 pc.can 谁能帮助我?
import java.util.Scanner;
public class Calculator{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please Enter 2 Values");
int userInput1 = input.nextInt();
int userInput2 = input.nextInt();
System.out.println("Please Enter an Operation");
String operator = input.next();
if(operator == null){
return;
}
int answer = 0;
switch (operator){
case "+":
answer = Add(userInput1,userInput2);
break;
case "*":
answer = Multiply(userInput1,userInput2);
break;
case "-":
answer = Subtract(userInput1,userInput2);
break;
case "/":
answer = Divide(userInput1,userInput2);
break;
default:
System.out.println("Invalid Operator");
System.exit(0);
break;
}
System.out.println("The answer is " + answer);
}
public static int Add(int num1, int num2)
{
return num1 + num2;
}
public static int Subtract(int num1, int num2)
{
return num1 - num2;
}
public static int Multiply(int num1, int num2)
{
return num1 * num2;
}
public static int Divide(int num1, int num2)
{
return num1 / num2;
}
}
这是我遇到的错误:
Calculator.java:23: incompatible types
found : java.lang.String
required: int
switch (operator){
^
1 error
在 String
上使用 switch
已在 Java 中介绍 7. 确保您在另一台机器上使用 JDK7+ 来编译程序。
如评论中所述,如果由于某种原因无法升级 JDK,则可以改用 char
:
String operator = input.next();
if(operator == null || operator.isEmpty()) {
return;
}
int answer = 0;
switch (operator.charAt(0)) {
case '+':
answer = Add(userInput1, userInput2);
break;
case '*':
answer = Multiply(userInput1, userInput2);
break;
...
我正在学习 java 并且我编写了这个程序,它在我大学的计算机上编译成功但在我家里编译不成功 pc.can 谁能帮助我?
import java.util.Scanner;
public class Calculator{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Please Enter 2 Values");
int userInput1 = input.nextInt();
int userInput2 = input.nextInt();
System.out.println("Please Enter an Operation");
String operator = input.next();
if(operator == null){
return;
}
int answer = 0;
switch (operator){
case "+":
answer = Add(userInput1,userInput2);
break;
case "*":
answer = Multiply(userInput1,userInput2);
break;
case "-":
answer = Subtract(userInput1,userInput2);
break;
case "/":
answer = Divide(userInput1,userInput2);
break;
default:
System.out.println("Invalid Operator");
System.exit(0);
break;
}
System.out.println("The answer is " + answer);
}
public static int Add(int num1, int num2)
{
return num1 + num2;
}
public static int Subtract(int num1, int num2)
{
return num1 - num2;
}
public static int Multiply(int num1, int num2)
{
return num1 * num2;
}
public static int Divide(int num1, int num2)
{
return num1 / num2;
}
}
这是我遇到的错误:
Calculator.java:23: incompatible types
found : java.lang.String
required: int
switch (operator){ ^
1 error
在 String
上使用 switch
已在 Java 中介绍 7. 确保您在另一台机器上使用 JDK7+ 来编译程序。
如评论中所述,如果由于某种原因无法升级 JDK,则可以改用 char
:
String operator = input.next();
if(operator == null || operator.isEmpty()) {
return;
}
int answer = 0;
switch (operator.charAt(0)) {
case '+':
answer = Add(userInput1, userInput2);
break;
case '*':
answer = Multiply(userInput1, userInput2);
break;
...