package thenewboston;
import java.util.Scanner;
public class apple {
public static void main(String args[]) {
Scanner myScan = new Scanner(System.in);
System.out.println("Enter your first num");
double fnum, snum, answer;
String oper;
fnum = myScan.nextDouble();
System.out.println("Enter your second num");
snum = myScan.nextDouble();
System.out.println("Enter your operation");
if (oper.equals("+")) {
answer = fnum + snum;
} else if (oper.equals("-")){
answer = fnum - snum;
} else if (oper.equals("*")) {
answer = fnum * snum;
} else if (oper.equals("/")){
answer = fnum / snum;
} else {
System.out.println("Please choose a valid operation");
}
System.out.println("Your answer is: " + answer);
}
}
您好。我试图使用.equals()来创建一个基本的计算器,但是当我写oper.equals(“...”)时,oper上出现了一个错误。
答案 0 :(得分:0)
您应该为操作指定值。
String oper = "*";
答案 1 :(得分:0)
您可能会收到错误,因为在使用之前您从未真正将oper
设置为任何内容。
...
snum = myScan.nextDouble();
System.out.println("Enter your operation");
oper = myScan.nextLine(); //<-- You were missing this.
if (oper.equals("+")) {
answer = fnum + snum;
...
答案 2 :(得分:0)
您没有要求用户输入操作,因此,您的操作无法与您之后说的任何内容相匹配。