我正在用Java编写视频扑克,我需要问玩家是否要从他们的手中取出任何卡。我为此写了一个while循环,但是它不能像现在那样工作。如果有人能按正确的方向发送给我,我将不胜感激-我对Java还是陌生的。 (counter在那里,所以玩家取出的卡不超过5张)

    String response = "y";
    int counter = 0;
    System.out.println("Remove any cards?");
    System.out.println("Enter y for 'yes' and n for 'no'");
    response = input.nextLine();
    while((response != "n") && (counter < 5))
    {

        System.out.println("Enter the number of a card to be removed (1-5)");
        int l = input.nextInt();
        p.removeCard(p.getHand().get(l-1));
        p.addCard(cards.deal());
        cards.incrementTop();
        counter ++;
        System.out.println("Card removed. More? Type 'yes' or 'no'");
        String answer = input.nextLine();
        if (answer == "no")
            {
                response = "n";
            }
    }

最佳答案

您不能使用!===比较字符串。

使用.equals()

while(!("n".equals(response)) && (counter < 5))


这里也一样

if (answer == "no")


其他需要改进的地方


用户可以第一次写“ no”,以及现在如何完成操作

您要求用户输入“否”,但您检查“ n”

那N呢?是啊
那nO呢?没有? (提示:搜索.equalsIgnoreCase()


给您的另一个提示:对yes/no使用布尔变量,会更好,更容易:)

08-26 01:16
查看更多