应该有一个奇怪的错误,那是什么?

时间:2015-06-05 12:15:53

标签: java

两个概率具有相同的代码,但是当我想要添加时,文本"输入第一个数字"没有显示。我做错了什么?

import java.util.Scanner;
public class berketurer {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int command1;
        int number1;
        int number2;
        int result;
        System.out.print("for subtraction,press 1.for addition,press 2.");
        command1=input.nextInt();
        if (command1==1)
            System.out.print("enter the first number:");
            number1=input.nextInt();
            System.out.print("enter the second number:");
            number2=input.nextInt();
            result=number1-number2;
            System.out.printf("the result is=%d\n",result);
        if (command1==2)
            System.out.print("enter the first number;"); //here is the problem
            number1=input.nextInt();
            System.out.print("enter the second number;");
            number2=input.nextInt();
            result=number1+number2;
            System.out.printf("the result is=%d\n",result);

    }
}

2 个答案:

答案 0 :(得分:3)

你错过了if语句体中的{}大括号,你所拥有的只是缩进(由于Java不是Python,它不起作用)。

答案 1 :(得分:0)

import java.util.Scanner;
public class berketurer {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int command1;
        int number1;
        int number2;
        int result;
        System.out.print("for addition,press 1.for subtraction,press 2.");
        command1=input.nextInt();
        if (command1==1) { // <-- opening
            System.out.print("enter the first number:");
            number1=input.nextInt();  // <- your code stops here
            System.out.print("enter the second number:");
            number2=input.nextInt();
            result=number1-number2;
            System.out.printf("the result is=%d\n",result);
        } // <-- closing
        if (command1==2) { // <-- opening
            System.out.print("enter the first number;"); //here is the problem
            number1=input.nextInt();
            System.out.print("enter the second number;");
            number2=input.nextInt();
            result=number1+number2;
            System.out.printf("the result is=%d\n",result);
    } // <-- closing
}

}

你忘记了每个街区的开启和关闭。因此,第一次打印不会执行,您的代码会停止等待输入。