在学校我不得不做一个计算器程序。在程序中,我们询问用户是否要加、减、乘或除。最后,我们询问用户是否要继续该程序。我还没有放入循环部分,但我这里的问题是显示“你想继续”之后,程序就退出了。
package calculator;
import java.util.Scanner;
public class calculator {
    public static void main(String[] args) {
        {
        int o1; //first operand
        int o2; //second operand
        Scanner input = new Scanner (System.in);
        System.out.println("Enter a choice:");
        System.out.println("+ to add");
        System.out.println("- to subtract");
        System.out.println("* to multiply");
        System.out.println("/ to divide");
        System.out.println("X to exit");
        String userChoice = input.nextLine();
        if (userChoice.equals("X to exit")) {
            System.exit(0);
        }
        System.out.println("Enter the first operand:");
        o1 = input.nextInt();
        System.out.println("Enter the second operand:");
        o2 = input.nextInt();
        if (userChoice.equals("+ to add")) {
            System.out.println( (o1) + (o2) ); }
            else if (userChoice.equals("- to subtract")) {
                System.out.println( (o1) - (o2) ); }
            else if (userChoice.equals("* to multiply")) {
                System.out.println( (o1) * (o2) ); }
            else if (userChoice.equals("/ to divide")) {
                System.out.println( (o1) / (o2) ); }
        System.out.println("Would you like to continue?");
        System.out.println("Yes");
        System.out.println("No");
        String userPick = input.nextLine(); {
        if (userPick.equals("Yes")) {
            System.out.println("Ok."); }
            else if (userPick.equals("No")) {
                System.exit(0); }
        }
        }
        }
        // TODO Auto-generated method stub
}