java - Else If Syntax Error in Calculator -


this question has answer here:

i've been trying make basic calculator, thing every time use if else, either says have remove due syntax error or green underline thing shows , says dead code. me?

import java.util.scanner;  public class additioncalculator {  public static void main(string[] args) {     int addresult;     int subtractresult;     int divideresult;     int multiplyresult;      string addition = null;     string subtraction = null;     string division = null;     string multiplication = null;      scanner reader = new scanner(system.in);     system.out.println("do want subtraction, division, multiplication, or addition?");     string = reader.next();      if(i == addition)   {         scanner additioncalc = new scanner (system.in);         system.out.println("enter number");         int add1 = reader.nextint();          scanner additioncalc2 = new scanner (system.in);         system.out.println("enter number");         int add2 = reader.nextint();          addresult = add1 + add2;          system.out.println("the sum " +addresult);        }   else if (i == multiplication) {         scanner multiplicationcalc = new scanner (system.in);         system.out.println("enter number");         int multiply1 = reader.nextint();          scanner multiplcationcalc2 = new scanner (system.in);         system.out.println("enter number");         int multiply2 = reader.nextint();          multiplyresult = multiply1 * multiply2;          system.out.println("the product " +multiplyresult);      } else if (i == division) {         scanner divisioncalc = new scanner (system.in);         system.out.println("enter number");         int div1 = reader.nextint();          scanner divisioncalc2 = new scanner (system.in);         system.out.println("enter number");         int div2 = reader.nextint();          divideresult = div1 * div2;         system.out.println("the product " +divideresult);       } else if (i == subtraction) {         scanner subtractioncalc = new scanner (system.in);         system.out.println("enter number");         int subtract1 = reader.nextint();          scanner subtractioncalc2 = new scanner (system.in);         system.out.println("enter number");         int subtract2 = reader.nextint();          subtractresult = subtract1 - subtract2;         system.out.println("the difference " +subtractresult);     } }   // addition calculator // scanner reader = new scanner(system.in); // system.out.println("please enter number.");         // int = reader.nextint(); // scanner reader2 = new scanner(system.in); // system.out.println("please enter number."); // int j = reader2.nextint();  // sum = + j; // system.out.println("the sum of 2 numbers " +sum); 

}

you should using string comparison function java.lang.string.

note:

== tests reference equality.

.equals() tests value equality.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -