【问题标题】:While loop w/ if statement won't follow sucesive if statements带有 if 语句的 while 循环不会跟随连续的 if 语句
【发布时间】:2015-08-03 10:31:14
【问题描述】:

我正在做一个我在互联网上找到的模拟游戏“nim”的练习。游戏的想法是让其他玩家拿起最后一件物品。我遇到的问题是在玩家 1 选择要从哪一堆中挑选以及从那堆中挑选多少物品之后。最初效果很好,但是当轮到玩家 2 时,总是选择他之前的玩家堆,而不管他们选择的堆,它会从与玩家 1 相同的堆中扣除他们指定的数量。这只发生在我添加了较低的 - if 语句中的 case 选项。有人可以解释为什么它总是选择以前的玩家堆吗?

while(pileA > 0 || pileB > 0 || pileC > 0){
    System.out.println(playerOne+", which pile would you like to pick from?");
    String choice = input.next();
    System.out.println("How many cards would you like to take from pile "+choice+"?");
    int amount = input.nextInt();
    if(choice.equals("A") || choice.equals("a")){
        pileA = pileA - amount;
        System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
    }
    else if(choice.equals("B") || choice.equals("b")){
        pileB = pileB - amount;
        System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
    }
    else if(choice.equals("C") || choice.equals("c")){
        pileC = pileC - amount;
        System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
    }
    else{
        System.out.println("ERROR");
    }



    System.out.println(playerTwo+", which pile would you like to pick from?");
    String choiceTwo = input.next();
    System.out.println("How many cards would you like to take from pile "+choiceTwo+"?");
    amount = input.nextInt();
    if(choiceTwo.equals("A") || choice.equals("a")){
        pileA = pileA - amount;
        System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
    }
    else if(choiceTwo.equals("B") || choice.equals("b")){
        pileB = pileB - amount;
        System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
    }
    else if(choiceTwo.equals("C") || choice.equals("c")){
        pileC = pileC - amount;
        System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
    }
    else{
        System.out.println("ERROR");
    }
    }

【问题讨论】:

  • 你试过调试了吗?
  • 您在第二个块中使用的是choice.equals 而不是choiceTwo.equals
  • @Shekhar 是的,我以为我做到了,但我错过了一些非常明显的东西。不过谢谢

标签: java string if-statement while-loop


【解决方案1】:

错在

       if(choiceTwo.equals("A") || choice.equals("a")){
    pileA = pileA - amount;

您应该使用choiceTwo 而不是第二个玩家的选择。

【讨论】:

    【解决方案2】:

    你正在检查 player1s 的输入,当你应该检查 player2s 的小写输入时:

    if(choiceTwo.equals("A") || choice.equals("a")){ // should be || choiceTwo.equals("a")
        pileA = pileA - amount;
        System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
    }
    else if(choiceTwo.equals("B") || choice.equals("b")){ // should be || choiceTwo.equals("b")
        pileB = pileB - amount;
        System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
    }
    else if(choiceTwo.equals("C") || choice.equals("c")){ // should be || choiceTwo.equals("c")
        pileC = pileC - amount;
        System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
    }
    

    【讨论】:

      【解决方案3】:

      当您添加播放器两个选项时,您似乎复制了 if 语句并忘记将小写选项的 choice.equals 转换为 choiceTwo.equals

      if(choiceTwo.equals("A") || choiceTwo.equals("a")){
              pileA = pileA - amount;
              System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
      }
      else if(choiceTwo.equals("B") || choiceTwo.equals("b")){
              pileB = pileB - amount;
              System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
      }
      else if(choiceTwo.equals("C") || choiceTwo.equals("c")){
              pileC = pileC - amount;
              System.out.println("A: "+pileA+"   B: "+pileB+"   C: "+pileC);
      }
      

      【讨论】:

        猜你喜欢
        • 2015-05-19
        • 2012-12-02
        • 2021-03-18
        • 1970-01-01
        • 2018-01-25
        • 2021-03-23
        • 2013-06-09
        • 2014-01-21
        • 2015-11-08
        相关资源
        最近更新 更多