在将输入作为运行时输入时未获得正确的结果

时间:2018-04-11 14:02:41

标签: java

enter image description here

我是java的新手。我已经尝试输入作为参数,那时执行是完美的。当我尝试将输入作为运行时输入时,它只给出“else”结果。我做错了什么?以及如何解决这个问题?

1 个答案:

答案 0 :(得分: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");
    }