簡體   English   中英

Java數組索引越界越好?

[英]Java Array Index Out of Bounds somehow?

在我的游戲代碼中,我嘗試添加一張手牌。 一旦我做完了,我的數組就超出了范圍。 一切看起來都不錯,但也許我缺少了一些東西。

僅供參考,一個和兩個是Player實例。 來自Main類的相關代碼(對格式感到抱歉。我很想將其傳輸到Stack Overflow):

import java.util.*;

public class Program {

    public static void main(String args[]) {
        String[] rank = {"two", "three", "four", "five", "six", "seven", "eight",
                     "nine", "ten", "jack", "queen", "king", "ace"};
        String[] suit = {"hearts", "diamonds", "spades", "clubs"};
        Scanner scan = new Scanner(System.in);
        String something = "yes", something2 = "yes"; //Use with while loop
        String winner = "yes"; //Use for while loop
        String temp; //Use with setting names
        Card[] deck = new Card[52]; //Deck array
        int playercount = 0;
        Player one = new Player("temp");
        Player two = new Player("temp");
        Player three = new Player("temp");
        Player four = new Player("temp");

        while (something2.equals("yes") || playercount < 2) {  //Add players to game

            System.out.println("Would a(nother) player like to join?");
            something2 = scan.nextLine();
            System.out.println();
            if (something2.equals("yes")) {
                if (playercount <= 4) {
                    if (playercount == 0) {
                        System.out.println("What is your name: ");
                        Player one1 = new Player(scan.nextLine());
                        one = one1;
                        playercount++;
                        System.out.println();
                    }
                    else if (playercount == 1) {
                        System.out.println("What is your name: ");
                        Player two2 = new Player(scan.nextLine());
                        two = two2;
                        playercount++;
                        System.out.println();
                    }
                    else if (playercount == 2) {
                        System.out.println("What is your name: ");
                        Player three3 = new Player(scan.nextLine());
                        three = three3;
                        playercount++;
                        System.out.println();
                    }
                    else if (playercount == 3) {
                        System.out.println("What is your name: ");
                        Player four4 = new Player(scan.nextLine());
                        four = four4;
                        playercount++;
                        System.out.println();
                    }
                    else {System.out.println("Only four players are allowed.");
                        something2 = "no";}
                }
            }
            else if (playercount < 2) {
                System.out.println("You need at least two players...");
                System.out.println();
            }
            else something2 = "no";
        }

        //Start game
        while (something.equals("yes")) {
            //Prepare game
            Card.makeDeck(deck, rank, suit);
            deck = Card.getDeck();
            Card.shuffle(deck);
            deck = Card.getDeck();

            //Deal cards
            if (playercount == 2) {
                for (int i = 1; i < 8; i++) {
                    one.addCard(Card.draw(deck));
                    deck = Card.getDeck();
                    two.addCard(Card.draw(deck));
                    deck = Card.getDeck();
                }
            }
            else if (playercount == 3) {
                for (int i = 1; i < 8; i++) {
                    one.addCard(Card.draw(deck));
                    deck = Card.getDeck();
                    two.addCard(Card.draw(deck));
                    deck = Card.getDeck();
                    three.addCard(Card.draw(deck));
                    deck = Card.getDeck();
                }
            }
            else {
                for (int i = 1; i < 8; i++) {
                    one.addCard(Card.draw(deck));
                    deck = Card.getDeck();
                    two.addCard(Card.draw(deck));
                    deck = Card.getDeck();
                    three.addCard(Card.draw(deck));
                    deck = Card.getDeck();
                    four.addCard(Card.draw(deck));
                    deck = Card.getDeck();
                }
            }
        }
    }
}

卡類:

import java.util.*;

public class Card {   
    private String suit;
    private String rank;
    private static int temp = 0, temp2 = 0; //Use for reseting rank and suit
    private static Card temp3; //Use for draw method
    private static int temp4; //Use for shuffle method
    private static Card[] deck = new Card[52];

    //Constructors
    public Card() {
        this.rank = "two";
        this.suit = "hearts";
    }
    public Card(String r, String s) {
        this.rank = r;
        this.suit = s;
    }

    //Mutators
    //Make deck
    public static void makeDeck(Card[] c, String[] r, String[] s) {
        for (int i = 0; i < c.length; i++) {
            c[i] = new Card(r[temp], s[temp2]);
            temp++; temp2++;
            //Reset rank and suit
            if (temp > 12)
                temp = 0;
            if (temp2 > 3)
                temp2 = 0;
        }
        deck = c;
    }

    //Accessors
    //Return deck
    public static Card[] getDeck() {
        return deck;   
    }
    //Shuffle
    public static Card[] shuffle(Card[] c) {
        for (int i = 0; i < c.length; i++) {
            int rand = (int)(Math.random()*(i + 1));
            //Don't let anything be in a slot that doesn't exist
            while (rand > c.length) {
                temp4 = (int)Math.random();
                rand -= temp4;
            }
            if (rand < 0)
                rand += temp4;
                Card temp = c[i];
                c[i] = c[rand];
                c[rand] = temp;
        }
        deck = c;
        return deck;  
    }
    //Draw
    public static Card draw(Card[] c) {
        if (c != null) {
        for (int i = 0; i < c.length; i++) {
        if (c[i] != null) {
            try {
                    return c[i];
                } finally {
                    c[i] = null;} //Remove i from c
            }
        }
        }    
        return null;
    }
}

玩家等級:

import java.util.*;

public class Player {
    private String name;
    private Card[] hand = new Card[52];
    private int handsize = 0; 

    //Constructor
    public Player(String n) {
    name = n;
    }

    //Mutators
    public void addCard(Card c) {
        hand[handsize] = c;
        handsize++;
    }

    //Accessors
    public String getName() {
        return name;   
    }
    public Card[] getHand() {
        return hand;   
    }
}

您的繪制方法已損壞。

// get the first non-null Card from the cards "c".
public static Card draw(Card[] c) {
  if (c != null) {
    for (int i = 0; i < c.length; i++) {
      if (c[i] != null) {
        try {
          return c[i];
        } finally {
          // now remove element i from the `c` array.
          c[i] = null;
        }
      }
    }
  }
  return null;
}

我認為您的代碼順序不正確(此代碼很難說明)

for (int i = 1; i < 8; i++)
{
 one.addCard(Card.draw(deck));
 deck = Card.getDeck();
 two.addCard(Card.draw(deck));
 deck = Card.getDeck();
}

也許應該是

for (int i = 1; i < 8; i++)
{
 deck = Card.getDeck();
 one.addCard(Card.draw(deck));
 deck = Card.getDeck();
 two.addCard(Card.draw(deck));
}

更新

public void addCard(Card c) {
    hand[handsize] = c;
    handsize++;
}

handsize從不增加-始終為0

問題出在你的循環上

while (something.equals("yes"))

有沒有什么設置的something為任何其他值,所以這個循環只是善有善報不休,直到所有的球員都超過52卡。 一旦某人擁有52張以上的卡,則添加新卡將導致異常。

我想你需要刪除此while 其中的代碼只能運行一次。

暫無
暫無

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

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