Java 错误:不兼容的类型:无法将字符转换为字符串
我做错了什么。我的名为operation的运算符已经是字符串 javac算术2.javaJava 错误:不兼容的类型:无法将字符转换为字符串,java,Java,我做错了什么。我的名为operation的运算符已经是字符串 javac算术2.java // Arithmetic2.java - This program performs arithmetic, ( +. -, *. /, % ) on two numbers // Input: Interactive. // Output: Result of arithmetic operation import javax.swing.*; public class Arithmet
// Arithmetic2.java - This program performs arithmetic, ( +. -, *. /, % ) on two numbers
// Input: Interactive.
// Output: Result of arithmetic operation
import javax.swing.*;
public class Arithmetic2
{
public static void main(String args[])
{
double numberOne, numberTwo;
String numberOneString, numberTwoString;
String operation;
double result;
numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
numberOne = Double.parseDouble(numberOneString);
numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
numberTwo = Double.parseDouble(numberTwoString);
operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");
// Call performOperation method here
performOperation(numberOne, numberTwo, operation, result);
System.out.format("%.2f",numberOne);
System.out.print(" " + operation + " ");
System.out.format("%.2f", numberTwo);
System.out.print(" = ");
System.out.format("%.2f", result);
System.exit(0);
} // End of main() method.
// Write performOperation method here.
public static double performOperation(double numberOne, double numberTwo, String operation, double result) {
double add = numberOne+numberTwo;
double sub = numberOne-numberTwo;
double mul = numberOne*numberTwo;
double div = numberOne/numberTwo;
double per = numberOne%numberTwo;
switch(operation) {
case '+' : { result = add; }
case '-' : { result = sub; }
case '*' : { result = mul; }
case '/' : { result = div; }
case '%' : { result = per; }
default : { System.out.println("Invalid operator!"); }
}
}
} // End of Arithmetic2 class.
算术2.java
// Arithmetic2.java - This program performs arithmetic, ( +. -, *. /, % ) on two numbers
// Input: Interactive.
// Output: Result of arithmetic operation
import javax.swing.*;
public class Arithmetic2
{
public static void main(String args[])
{
double numberOne, numberTwo;
String numberOneString, numberTwoString;
String operation;
double result;
numberOneString = JOptionPane.showInputDialog("Enter the first number: ");
numberOne = Double.parseDouble(numberOneString);
numberTwoString = JOptionPane.showInputDialog("Enter the second number: ");
numberTwo = Double.parseDouble(numberTwoString);
operation = JOptionPane.showInputDialog("Enter an operator (+.-.*,/,%): ");
// Call performOperation method here
performOperation(numberOne, numberTwo, operation, result);
System.out.format("%.2f",numberOne);
System.out.print(" " + operation + " ");
System.out.format("%.2f", numberTwo);
System.out.print(" = ");
System.out.format("%.2f", result);
System.exit(0);
} // End of main() method.
// Write performOperation method here.
public static double performOperation(double numberOne, double numberTwo, String operation, double result) {
double add = numberOne+numberTwo;
double sub = numberOne-numberTwo;
double mul = numberOne*numberTwo;
double div = numberOne/numberTwo;
double per = numberOne%numberTwo;
switch(operation) {
case '+' : { result = add; }
case '-' : { result = sub; }
case '*' : { result = mul; }
case '/' : { result = div; }
case '%' : { result = per; }
default : { System.out.println("Invalid operator!"); }
}
}
} // End of Arithmetic2 class.
您需要将单引号替换为双引号,因为Java将单引号解释为字符,因此您的代码需要读取:
switch(operation) {
case "+" : {result = add; } // and so on...
}
希望这有点帮助:-)将
操作
变量转换为字符
类型,它就可以工作了
或者通过将单引号替换为双引号将开关选项更改为字符串,因为它会将选项更改为
String
是的,操作
是字符串,但错误告诉您'+'
不是字符串
在Java中,双引号用于生成字符串文字,单引号用于生成字符文字。您正在尝试将字符串与字符进行比较
如果将“+”
(以及大小写的其余操作数)更改为“+”
,它应该可以工作。因为在大多数情况下返回字符串
(除了需要大量参数来帮助它返回正确的对象类型),最好在后续流中使用字符串
只需将开关
cases引号从'
(char)更改为'
(String)。您不需要更改方法参数
switch(operation) {
case "+" : { result = add; break; }
case "-" : { result = sub; break; }
case "*" : { result = mul; break; }
case "/" : { result = div; break; }
case "%" : { result = per; break; }
default : { System.out.println("Invalid operator!"); break; }
}
编辑:不要忘记break
s.操作
是一个字符串
,但是您的case
语句都使用字符
常量。选项1:更改开关
,如
switch(operation.charAt(0))
选项2:更改case
语句,如
switch(operation) {
case "+" : { result = add; }
case "-" : { result = sub; }
case "*" : { result = mul; }
case "/" : { result = div; }
case "%" : { result = per; }
default : { System.out.println("Invalid operator!"); }
}
但是,在这两种情况下,{}
都没有意义,case
语句将通过-添加中断
switch(operation) {
case "+" : result = add; break;
case "-" : result = sub; break;
case "*" : result = mul; break;
case "/" : result = div; break;
case "%" : result = per; break;
default : System.out.println("Invalid operator!"); break;
}
正如上面所说的:您正试图将字符串
(操作
)与字符
(切换案例)进行比较。将字符
更改为字符串
,反之亦然。是的,这非常有意义。我不知道我是怎么错过的。谢谢