Java数组索引越界不知何故呢? [英] Java Array Index Out of Bounds somehow?

查看:150
本文介绍了Java数组索引越界不知何故呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的游戏的code,我试图将卡添加到手上。当我这样做,我的数组是出界。一切看起来都对,但也许我失去了一些东西。

FYI,一和二是Player实例。从主类相关code(约格式化对不起,我吮吸转移它堆栈溢出。)

 进口的java.util。*;公共类节目{    公共静态无效的主要(字符串ARGS []){
        的String [] =等级{二,三,四有,五,六个一,七,八,
                     九,十,杰克,皇后,王,王牌};
        的String [] =套装{心,钻石,黑桃,俱乐部};
        扫描程序扫描=新的扫描仪(System.in);
        字符串的东西=YES,something2 =是; //使用whil​​e循环使用
        字符串赢家=是; //使用whil​​e循环
        串温度; //使用设置名称使用
        卡[] =甲板新卡[52]; //甲板阵列
        INT playercount = 0;
        球员1 =新播放器(TEMP);
        球员2 =新播放器(TEMP);
        玩家将3 =新播放器(TEMP);
        播放器4 =新播放器(TEMP);        而(something2.equals(是)|| playercount 2){//添加玩家游戏            的System.out.println(会(另)一个球员想加入?);
            something2 = scan.nextLine();
            的System.out.println();
            如果(something2.equals(是)){
                如果(playercount&下; = 4){
                    如果(playercount == 0){
                        的System.out.println(你叫什么名字:);
                        球员one1 =新播放器(scan.nextLine());
                        1 = one1;
                        playercount ++;
                        的System.out.println();
                    }
                    否则如果(playercount == 1){
                        的System.out.println(你叫什么名字:);
                        球员two2 =新播放器(scan.nextLine());
                        2 = two2;
                        playercount ++;
                        的System.out.println();
                    }
                    否则如果(playercount == 2){
                        的System.out.println(你叫什么名字:);
                        球员three3 =新播放器(scan.nextLine());
                        3 = three3;
                        playercount ++;
                        的System.out.println();
                    }
                    否则如果(playercount == 3){
                        的System.out.println(你叫什么名字:);
                        球员four4 =新播放器(scan.nextLine());
                        4 = four4;
                        playercount ++;
                        的System.out.println();
                    }
                    其他{的System.out.println(只有四名球员是允许的。);
                        something2 =无;}
                }
            }
            否则如果(playercount 2){
                的System.out.println(你需要至少两名球员......);
                的System.out.println();
            }
            别的something2 =无;
        }        //开始游戏
        而(something.equals(是)){
            // prepare游戏
            Card.makeDeck(甲板,等级,花色);
            甲板= Card.getDeck();
            Card.shuffle(甲板);
            甲板= Card.getDeck();            //交易卡
            如果(playercount == 2){
                的for(int i = 1; I< 8;我++){
                    one.addCard(Card.draw(甲板));
                    甲板= Card.getDeck();
                    two.addCard(Card.draw(甲板));
                    甲板= Card.getDeck();
                }
            }
            否则如果(playercount == 3){
                的for(int i = 1; I< 8;我++){
                    one.addCard(Card.draw(甲板));
                    甲板= Card.getDeck();
                    two.addCard(Card.draw(甲板));
                    甲板= Card.getDeck();
                    three.addCard(Card.draw(甲板));
                    甲板= Card.getDeck();
                }
            }
            其他{
                的for(int i = 1; I< 8;我++){
                    one.addCard(Card.draw(甲板));
                    甲板= Card.getDeck();
                    two.addCard(Card.draw(甲板));
                    甲板= Card.getDeck();
                    three.addCard(Card.draw(甲板));
                    甲板= Card.getDeck();
                    four.addCard(Card.draw(甲板));
                    甲板= Card.getDeck();
                }
            }
        }
    }
}

卡类:

 进口的java.util。*;公共类卡{
    私人字符串套装;
    私人字符串排名;
    私有静态诠释临时= 0,TEMP2 = 0; //使用为CH375复位点数和花色
    私有静态卡TEMP3; //使用的绘制方法
    私有静态诠释temp4; //使用的洗牌方法
    私有静态卡[] =甲板新卡[52];    //构造函数
    市民卡(){
        this.rank =二;
        this.suit =心;
    }
    市民卡(字符串R,一个String){
        this.rank = R;
        this.suit =秒;
    }    //存取器
    //使甲板
    公共静态无效makeDeck(卡[] C,的String [] R,的String [] S){
        的for(int i = 0; I< c.length;我++){
            C [i] =新卡(R [临时],S [TEMP2]);
            临时++; TEMP2 ++;
            //重置点数和花色
            如果(温度> 12)
                温度= 0;
            如果(TEMP2→3)
                TEMP2 = 0;
        }
        甲板= C;
    }    //访问器
    //返回甲板
    公共静态卡[] getDeck(){
        回到甲板上;
    }
    //洗牌
    公共静态卡[]洗牌(卡[] C){
        的for(int i = 0; I< c.length;我++){
            INT兰特=(int)的(的Math.random()*第(i + 1));
            //不要让任何事情是不存在的插槽
            而(RAND> c.length){
                temp4 =(INT)的Math.random();
                兰德 - = temp4;
            }
            如果(兰特℃,)
                兰德+ = temp4;
                卡温度= C [I]
                C [i] = C [兰德]
                C [兰德] =温度;
        }
        甲板= C;
        回到甲板上;
    }
    //画
    公共静态抽奖卡(卡[] C){
        如果(C!= NULL){
        的for(int i = 0; I< c.length;我++){
        如果(C [I]!= NULL){
            尝试{
                    返回C [I];
                } {最后
                    C [i] = NULL;} //从C删除我
            }
        }
        }
        返回null;
    }
}

Player类:

 进口的java.util。*;公共类播放器{
    私人字符串名称;
    私人卡[] =手新卡[52];
    私人INT handsize = 0;    //构造
    公众播放器(字符串n){
    名称= N;
    }    //存取器
    公共无效addCard(卡C){
        手[handsize] = C;
        handsize ++;
    }    //访问器
    公共字符串的getName(){
        返回名称;
    }
    市民卡[] getHand(){
        返回手;
    }
}


解决方案

问题是与你的循环

 而(something.equals(是))

有没有什么设置的东西为其他任何值的,所以这个循环只是绕着不休,直到所有的球员都超过52张。一旦某人有超过52张牌,加入了新卡会导致异常。

我认为你需要删除这个,而。它里面的code应该只运行一次。

In my game's code, I am trying to add a card to hand. As soon as I do, my array is out of bounds. Everything looks right, but maybe I'm missing something.

FYI, one and two are Player instances. Relevant code from Main class (sorry about the formatting. i suck at transferring it to 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();
                }
            }
        }
    }
}

Card class:

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;
    }
}

Player class:

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;   
    }
}

解决方案

The problem is with your loop

while (something.equals("yes"))

There's nothing that sets something to any other value, so this loop just goes around endlessly, until all the players have more than 52 cards. Once someone has more than 52 cards, adding a new card causes the exception.

I think you need to remove this while. The code inside it should only be run once.

这篇关于Java数组索引越界不知何故呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆