-1

enter image description here

I am new to java. i have tried input as arguments at that time there is execution is perfect. When i am try to give input as runtime input it gives only "else" result. what is the mistake i have done? and How to solve this?

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
KarthiK
  • 71
  • 2
  • 10
  • 1
    Posting pictures is ok but please post your code as text if you ask a question so it´s possible to copy it. – LenglBoy Apr 11 '18 at 14:04
  • 1
    you can't compare strings with == use .equals() – dave Apr 11 '18 at 14:04
  • On top of what the other comments said, you can also add print statements right after the input appeared, to output what the input was. To confirm you got what you think you got. – Koen De Groote Apr 11 '18 at 14:06
  • `==` does compare the object reference so if this is the same field on your space. You need to use `string.equals(string)` to compare values of a string. Of cause you can use `==` for non-object/primitiv types/values – LenglBoy Apr 11 '18 at 14:07
  • 1
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jacob G. Apr 11 '18 at 14:07

1 Answers1

0
public void h(){
    Scanner s = new Scanner(System.in);
    System.out.println("What you Want to do = ");
    String k = s.nextLine();
    System.out.println("Enter the value of a = ");
    int a = s.nextInt();
    System.out.println("Enter the value of b = ");
    int b = s.nextInt();

    if(("p").equals(k) || (("+").equals(k))){
       sum = a+b;
        System.out.println("Sum = "+sum);
    }
    else if(("s").equals(k) || (("-").equals(k))){
        sum = a-b;
        System.out.println("Sum = "+sum);
    }
    else if(("m").equals(k) || (("*").equals(k))){
        sum = a*b;
        System.out.println("Sum = "+sum);
    }
    else if(("d").equals(k) || (("/").equals(k))){
        sum = a/b;
        System.out.println("Sum = "+sum);
    }else{
        System.out.println("Enter the correct work to do");
    }
Elarbi Mohamed Aymen
  • 1,617
  • 2
  • 14
  • 26