Java - 如果语句逻辑错误

时间:2015-09-24 19:03:49

标签: java

事件的顺序应为:

  

1)我输入两个。
  2)我被提示选择一个名字   3)我选择华盛顿/富兰克林/汉密尔顿   4)我被问到这个名字出现在哪个面额上   5)我给出答案。

但是,当我进入华盛顿参加第三部分时 - 我被告知这是一个无效的数字。我不明白为什么会这样。

public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("Type 1 to enter a denomination, 2 to enter a last name");
    Scanner in = new Scanner(System.in);
    int x = in.nextInt();

    if(x==1){
        System.out.println("Choose a denomination");
        int y = in.nextInt();
        in.nextLine();
        if(y==1){
            System.out.println("Which person appears on the 1 bill?");
            String answer = in.nextLine();
            if(answer.equals("Washington")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }


        else if(y==10){
            System.out.println("Which person appears on the 10 bill?");
            String answer = in.nextLine();
            if(answer.equals("Hamilton")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else if(y==100){
            System.out.println("Which person appears on the 100 bill?");
            String answer = in.nextLine();
            if(answer.equals("Franklin")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else{
            System.out.println("That is an invalid number.");
        }
    }
    else if(x==2){
       System.out.println("Choose a name");
        String y = in.nextLine();
        in.nextLine();
        if(y.equals("Washington")){
            System.out.println("Which denomination does this name appear on?");
            int answer = in.nextInt();
            if(answer==1){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }


        else if(y.equals("Hamilton")){
            System.out.println("Which denomination does this name appear on");
            int answer = in.nextInt();
            if(answer==10){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else if(y.equals("Franklin")){
            System.out.println("Which denomination does this name appear on");
            int answer = in.nextInt();
            if(answer==100){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else{
            System.out.println("That is an invalid number.");
        }
    } 
}

问题在于x == 2段。 x == 1工作正常。

4 个答案:

答案 0 :(得分:1)

问题依赖于看似额外的in.nextLine()

我会建议您将所有in.nextInt()更改为in.nextLine() ,然后将其解析,改为实际类型,例如int

示例:

int answer = Integer.parseInt(in.nextLine());

建议更改的原因:当您使用nextInt()时,有一种趋势是,下一行仍然会在那里徘徊,直到您执行额外的nextLine()清除它。 / p>

为防止出现此类问题,建议您使用nextLine()接收所有输入,然后将其解析为实际类型(intdouble等。)

这也是 Microsoft 处理C#中整数输入的方式。

已编辑的工作代码:

public static void main(String[] args) {

    System.out.println("Type 1 to enter a denomination, 2 to enter a last name");
    Scanner in = new Scanner(System.in);
    int x = Integer.parseInt(in.nextLine());

    if(x==1){
        System.out.println("Choose a denomination");
        int y = Integer.parseInt(in.nextLine());
        in.nextLine();
        if(y==1){
            System.out.println("Which person appears on the 1 bill?");
            String answer = in.nextLine();
            if(answer.equals("Washington")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }


        else if(y==10){
            System.out.println("Which person appears on the 10 bill?");
            String answer = in.nextLine();
            if(answer.equals("Hamilton")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else if(y==100){
            System.out.println("Which person appears on the 100 bill?");
            String answer = in.nextLine();
            if(answer.equals("Franklin")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else{
            System.out.println("That is an invalid number.");
        }
    }
    else if(x==2){
       System.out.println("Choose a name");
        String y = in.nextLine();
        //in.nextLine();
        if(y.equals("Washington")){
            System.out.println("Which denomination does this name appear on?");
            int answer = Integer.parseInt(in.nextLine());
            if(answer==1){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }


        else if(y.equals("Hamilton")){
            System.out.println("Which denomination does this name appear on");
            int answer = Integer.parseInt(in.nextLine());
            if(answer==10){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else if(y.equals("Franklin")){
            System.out.println("Which denomination does this name appear on");
            int answer = Integer.parseInt(in.nextLine());
            if(answer==100){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else{
            System.out.println("That is an invalid number.");
        }
    } 
}

答案 1 :(得分:0)

String y = in.nextLine();
in.nextLine();
if(y.equals("Washington")){

您多次调用in.nextLine()。删除第二行。

答案 2 :(得分:0)

您必须在nextInt()之后跳过该行,然后接受用户输入(在y==2情况下),因为nextInt()不会消耗它使用该令牌的整行。

in.nextLine();
String y = in.nextLine();

使用您的代码,in.nextLine()中的String y = in.nextLine();正在读取您输入x值的同一行,其值y"",因此输出"That is an invalid number"

答案 3 :(得分:0)

当您执行String y = in.nextLine()时,下一个标记是上一次nextInt()来电中的剩余换行符,因此y最终等于\n

使用nextLine()后打电话给nextInt()

int x = in.nextInt();
in.nextLine();

之后,您应该只能在获得用户输入时拨打nextLine一次:

String y = in.nextLine();
if(y.equals("Washington")){