簡體   English   中英

如何修復“線程“main”java.util.NoSuchElementException 中的異常”錯誤? 使用 LinkedList removeFirst() 和 Pop() 時

[英]How do I fix the "Exception in thread "main" java.util.NoSuchElementException" error? when using LinkedList removeFirst() and Pop()

我不知道錯誤來自哪里,或者至少不知道錯誤是從哪一行彈出的。 如果用戶在問題中連續輸入 1,則會出現錯誤。 如果我還輸入 1 和 2 的組合作為用戶輸入問題,那么也會出現錯誤。 如果您可以提供有關為什么會出現該錯誤的提示,那么我將能夠以某種方式解決它。

我已經嘗試使用 System.out.println 語句來指示問題發生的位置。 但仍然無法修復它。

import java.util.*;

public class War 
{

public static void main(String[] args) 
{
    ArrayList<Card> deck = new ArrayList<>();

    for(int s = 0; s < 4; s++) // 0 - 3 suits (4 suits)
    {
        for(int r = 2; r < 15; r++)// 2 - 14 ranks (13 ranks)
        {
            deck.add(new Card(s, r)); // creating a new card and adding it 
            t0 the deck
        }
    }

    Collections.shuffle(deck, new Random()); //shuffle the deck of cards 
    randomly

    //creating 2 decks, each for player1/player2
    LinkedList<Card> userP1 = new LinkedList<>();
    LinkedList<Card> computerP2 = new LinkedList<>();

    userP1.addAll(deck.subList(0, 26));            
    computerP2.addAll(deck.subList(26, deck.size()));

    System.out.println("The player 1 has " + userP1.size() + " cards in the 
    shuffled deck");
    System.out.println("The player 2 has " + computerP2.size() + " cards in 
    the shuffled deck");

    while(true)
    {
        Card p1Card = userP1.pop();  //each player place one card face up
        Card p2Card = computerP2.pop();

        //display the face up card
        System.out.println("Player 1 plays card is " + p1Card.toString());
        System.out.println("Player 2 plays card is " + p2Card.toString());

        //rank comparation between two cards
        if(p1Card.getRank() > p2Card.getRank()){//if player 1 win and 
        player 2 has lowest rank - Computer has the lower rank and its 
        behavior
            System.out.println("Computer's card has the lowest rank.");
            userP1.addLast(p2Card);  //places them at the bottom of the 
        users deck.
            System.out.println("Player 1 won the round.");
        }//end if

        else if(p1Card.getRank() < p2Card.getRank())
        {//if player 2 win and player 1 has the lowest rank
            Scanner input = new Scanner(System.in);
            System.out.println("Player 1 has two options:\n1. To lose the 
         card to the opposite player. "
                    + "\n2. Play the next card at the top of your own 
            deck.");
            System.out.println("Please choose your option: ");
            int a = input.nextInt();
            if(a == 1)
            {
                computerP2.addLast(p1Card);
                userP1.remove(p1Card);
            }
            else if(a == 2)
            {
                if(userP1.isEmpty())
                {
                    System.out.println("No more cards for the user. GAME 
                    OVER.");
                    break;
                }
                Card p1NextCard = userP1.pop();
                int sum = p1Card.getRank() + p1NextCard.getRank();
                if(sum <= p2Card.getRank())
                {
                    userP1.add(p2Card);
                }
                else if(sum > p2Card.getRank())
                {
                    computerP2.add(p1Card);
                    computerP2.add(p1NextCard);                    
                }
                if(sum == p2Card.getRank())
                {
                    System.out.println("Player 1 deck remaining cards: " + 
                    userP1.size());
                    System.out.println("Player 2 deck remaining cards: " + 
                    computerP2.size());
                    System.out.println("Time for WAR");
                    //creating war cards
                    ArrayList<Card> war1 = new ArrayList<>(); 
                    ArrayList<Card> war2 = new ArrayList<>();
                    //checking do players have enough (4)cards to stay in 
                    game
                    for(int x=0; x<3; x++)
                    { 
                        //either one player runs out of card is game over
                        if(userP1.isEmpty() || computerP2.isEmpty())
                        {                      
                        break;
                        }
                        System.out.println("War card for player 1 is xx 
                        \nWar card for player 2 is xx");

                        war1.add(3,userP1.pop());  //place additional card 
                        for war
                        war2.add(3,computerP2.pop());                  
                    }//end for
                    //only compare results when both players have enough 
                       cards for war
                        if(war1.size() == 3 && war2.size() == 3 )
                        {
                        //display the war cards from each player
                            System.out.println("War card for player 1 is " 
                            + war1.get(0).toString());
                            System.out.println("War card for player 2 is " 
                            + war2.get(0).toString());

                            //if player 1 wins the war round
                        if(war1.get(2).getRank() > war2.get(2).getRank())
                        {
                            userP1.addAll(war1); //player1 get all 10 cards
                            computerP2.addAll(war2);
                            System.out.println("Player 1 wins the war 
                            round");
                        }//end if
                        //otherwise player 2 wins the war round
                        else
                        {
                            computerP2.addAll(war1); //player2 get all 10 
                         cards
                            computerP2.addAll(war2);
                            System.out.println("Player 2 wins the war 
                            round");
                        }//end else
                        }//end if

                }//end war round else

                //game over either one player runs out of card(deck size is 
                0)
                if(userP1.isEmpty())
                {
                    System.out.println("game over\nPlayer 1 wins the 
                    game");
                    break;
                }
                else if(computerP2.isEmpty())
                {
                    System.out.println("game over\nPlayer 2 wins the 
                    game");
                    break;
                }
            }
        }
        else
        {
            System.out.println("WAR");
            ArrayList<Card> war1 = new ArrayList<>(); 
            ArrayList<Card> war2 = new ArrayList<>();
            //checking do players have enough (4)cards to stay in game
            for(int x=0; x<3; x++)
            { 
            //either one player runs out of card is game over
                if(userP1.isEmpty() || computerP2.isEmpty())
                {                      
                    break;
                }
                System.out.println("War card for player 1 is xx \nWar card 
                for player 2 is xx");

                war1.add(3,userP1.pop());  //place additional card for war
                war2.add(3,computerP2.pop());                  
            }//end for

        }
    }
}
}

程序拋出java.lang.IndexOutOfBoundsException:每次都不是java.util.NoSuchElementException java.lang.IndexOutOfBoundsException:被拋出,因為在

war1.add(3,userP1.pop()); //place additional card for war

您試圖在索引 3 處添加一個元素,但ArrayList的長度為 0(Java 8 中ArrayList的默認長度)。 要解決這個問題,只需像這樣刪除 index 參數。

war1.add(userP1.pop());

一旦這個和你犯這個錯誤的其他實例得到解決,程序開始拋出java.util.NoSuchElementException 這是來自

Card p1Card = userP1.pop();  //each player place one card face up
Card p2Card = computerP2.pop();

因為您試圖從空列表中獲取元素。 要解決此問題,請先檢查以確保在執行pop之前列表不為空。

if (!userP1.isEmpty()) {
  Card p1Card = userP1.pop();  //each player place one card face up
  Card p2Card = computerP2.pop();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM