Java 我怎样才能摆脱这个“错误”;输入错误“;我的回答中附带的声明

Java 我怎样才能摆脱这个“错误”;输入错误“;我的回答中附带的声明,java,Java,请检查为什么我的代码给出了一个“错误的输入”语句以及正确的答案。虽然我在else块中使用过它 import java.util.*; public class Encapsulation { public static void main(String args[]) throws InterruptedException { Scanner sc = new Scanner(System.in); System.out.print("Select operat

请检查为什么我的代码给出了一个“错误的输入”语句以及正确的答案。虽然我在else块中使用过它

import java.util.*;
public class Encapsulation {
    public static void main(String args[]) throws InterruptedException {
    Scanner sc = new Scanner(System.in);
    System.out.print("Select operation(1:add, 2: sub): ");
    int choose = sc.nextInt();
    int a, b;
    System.out.println("Enter 1st number: ");
    a = sc.nextInt();
    System.out.println("Enter 2nd number: ");
    b = sc.nextInt();
    choice(choose, a, b);

    System.out.println("Do you want to calculate again(y/n): ");
    char select = sc.next().charAt(0);
    if (select == 'y') {
        main(args);
    } else {
        System.out.println("Closing the program.........");
        Thread.sleep(1000);
        System.out.println("Program closed succesfully");
    }
    sc.close();
}
public static int add(int a, int b) {
    return a + b;
}

public static int diff(int a, int b) {
    return a - b;
}

public static void choice(int choose, int a, int b) {
    if (choose == 1) {
        System.out.println("The sum = " + add(a, b));
    }
    if (choose == 2) {
        System.out.println("The diff = " + diff(a, b));
    } else {
        System.out.println("Wrong input!");
    }
}
}

这是终端窗口的消息。我想删除出现的“错误输入”

Select operation(1:add, 2: sub): 1
Enter 1st number: 
10
Enter 2nd number: 
10
The sum = 20
Wrong input!    //This is the problem i don't want this statement
Do you want to calculate again(y/n): 

将您的
选择
方法更改为:

public static void choice(int choose, int a, int b) {
    if (choose == 1) {
        System.out.println("The sum = " + add(a, b));
    } else if (choose == 2) {  // <---- u shd use else-if here
        System.out.println("The diff = " + diff(a, b));
    } else {
        System.out.println("Wrong input!");
    }
}

只需将
if(choose==2){
更改为
else if(choose==2){
,因为1!=2所以调用了else。在choose==1BTW的if块中返回:调试器可以帮助您查找此类问题。非常感谢
Select operation(1:add, 2: sub): 1                                                                                                                          
Enter 1st number:                                                                                                                                           
10                                                                                                                                                          
Enter 2nd number:                                                                                                                                           
10                                                                                                                                                          
The sum = 20                                                                                                                                                
Do you want to calculate again(y/n):