所以我一直在努力尝试制作一个简单的计算器,同时也使用方法。出于某种原因检查字符串" func"等于" *"它会跳过并返回"未知"在插入其他功能时也会发生这种情况
package test;
import java.util.Scanner;
public class test {
public static float num1;
public static String func;
public static float num2;
public static float awnser;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Simple Calculator");
num1 = input.nextFloat();
func = input.next();
System.out.println(func); //Confirms func=="*"
num2 = input.nextFloat();
times();}
public static void times() {
if(func=="*"){awnser=num1*num2;}
else if(func=="/"){awnser=num1/num2;System.out.println("The awnser is: "+awnser);}
else if(func=="+"){awnser=num1+num2;System.out.println("The awnser is: "+awnser);}
else if(func=="-"){awnser=num1-num2;System.out.println("The awnser is: "+awnser);}
else {System.out.println("Unknown");}}}