So I have been messing around with this trying to make a simple calculator while also using methods. For some reason when checking if the string "func" is equal to "*" it skips and instead returns "Unknown" it also happens when imputing the other functions
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");}}}
OUTPUT:
- Simple Calculator
- 5
- *
- *
- 5
- Unknown
Aucun commentaire:
Enregistrer un commentaire