Java - Input and If statements -


i'm new programming java please bare me. i'm trying create command/console calculator program i'm getting stuck if statements operators.

i know isn't easiest (or correct) way create calculator program i'm doing way can practice user output, input, if statements , calculations.

i thinking operator variable type char, if way cant user input. whats wrong following code?

package calculator; import static java.lang.system.in ; import java.util.scanner;  public class calculator {    public static void main(string[] args) {       scanner in = new scanner(system.in);   system.out.println ("welcome calculator!");  system.out.print ("enter calculation: ");   int a;  int b;  string op; //operator    = in.nextint();  b = in.nextint();  op = in.next();   if (op == '*') { a*b ;}  if (op =='/') { a/b ;}   if (op == '+') {a+b;}   if (op == '-') {a-b;}     }  } 

i haven't finished off yet (as can see there isn't output) that's because cant if statements work.

the errors i'm getting are:

 error: not statement  if (op == '-') {a-b;}  

this every if statement. appreciated.

there few things need consider:

  1. op string need compare using .equals("op") or .equalsignorecase("op") method.
  2. you getting error not statement because need use ; terminate statement a-b not statement need using on following code.
  3. and last checking '*' single code uses char

you can see following code formatted language.

public class calculator {  public static void main(string[] args) {      scanner in = new scanner(system.in);      system.out.println("welcome calculator!");      int a;     int b;     string op; //operator     int c = 0;      system.out.println("enter first value :");     = in.nextint();     system.out.println("enter second value :");     b = in.nextint();     system.out.println("enter operator :");     op = in.next();      if (op.equals("*")) {         c = * b;     } else if (op.equals("/")) {         c = / b;     } else if (op.equals("+")) {         c = + b;     } else if (op.equals("-")) {         c = - b;     }      system.out.println("output " + c);  } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -