while循环中的奇怪行为

时间:2015-12-22 22:27:13

标签: java

我正在写一个在二十一点转弯的功能。它看起来像这样:

//here is the signature of the method;
public void remove (){
    Node<T> tmp = null;
    tmp.next = head;
    // I want to delete the current by the way!;
    while (tmp.next != current)
        tmp = tmp.next;
    tmp.next = current.next;
    //now am taking the current to the node before it so that it only becomes null if the linkedlist is empty;
    current=tmp;
}

奇怪的是,当我输入&#34; n&#34;时,它不会绘制另一张卡但 重新提示我。然后我抛出了一个printLine语句,如下所示:

public void takeTurn(Deck deck) {
    Scanner reader = new Scanner(System.in);
    String response = "y";
    while (response.toLowerCase().equals("y")) {
    System.out.print("Would you like another card? (Y or N) ");
        response = reader.nextLine();
        if (response.toLowerCase().equals("y")) {
            hand.getCards().add(deck.getTopCard());
            System.out.println("Current hand: " + hand);
            if (hand.getValue() > 21) {
                System.out.println("You busted with " + hand.getValue());
                break;
            }
        }           
    } 
}

这是一个示例互动:

public void takeTurn(Deck deck) {
    Scanner reader = new Scanner(System.in);
    String response = "y";
    while (response.toLowerCase().equals("y")) {
        System.out.println("response is " + response);
        System.out.print("Would you like another card? (Y or N) ");
        response = reader.nextLine();
        if (response.toLowerCase().equals("y")) {
            hand.getCards().add(deck.getTopCard());
            System.out.println("Current hand: " + hand);
            if (hand.getValue() > 21) {
                System.out.println("You busted with " + hand.getValue());
                break;
            }
        }           
    } 
}

第一个&#34;响应是y&#34;说得通。第二个我无法解释。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

public static void main(String[] args) {
    takeTurn(new Scanner(System.in)); // just an example how you share a single Scanner as a parameter when calling takeTurn function
}

public static void takeTurn(Scanner sc/*, Deck deck*/) { // static may be removed if you do not use the function within static main void
    if (isYResponse(sc, "Would you like another card? (Y or N) ")) {
        System.out.println("response is y");
        /*
        hand.getCards().add(deck.getTopCard());
        System.out.println("Current hand: " + hand);
        if (hand.getValue() > 21) {
            System.out.println("You busted with " + hand.getValue());
        }
        */
    } else {
        System.out.println("response is n");
    }
    takeTurn(sc/*, deck.next()*/); // be careful with this loop: define when it stops actually... when isGameOver(), for example?
}

private static boolean isYResponse(Scanner sc, String message) { // static may be removed if you do not use the function within static main void
    System.out.print(message);
    String response;
    if ((response = sc.nextLine()).isEmpty()) {
        response = sc.nextLine();
    }
    return ("y".compareToIgnoreCase(response) == 0)
            ? true
            : (("n".compareToIgnoreCase(response) == 0)
                ? false
                : isYResponse(sc, message));
}

P.S。对不起:我不知道其他课程的结构,例如Deck。我希望我的答案可以帮助您找到所需的最终具体解决方案。