mardi 17 mars 2015

If equals to not working [duplicate]


This question already has an answer here:




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:



  1. Simple Calculator

  2. 5

  3. *

  4. *

  5. 5

  6. Unknown


Aucun commentaire:

Enregistrer un commentaire