当前位置: 首页 > 面试题库 >

Java数组索引越界越好?

越信鸥
2023-03-14
问题内容

在我的游戏代码中,我尝试添加一张手牌。一旦我做完了,我的数组就超出了范围。一切看起来都不错,但也许我缺少了一些东西。

仅供参考,一个和两个是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;   
    }
}

问题答案:

问题出在你的循环上

while (something.equals("yes"))

没有something其他任何值可设置,因此此循环不断循环,直到所有玩家拥有超过52张牌为止。一旦某人拥有52张以上的卡,添加新卡将导致异常。

我认为您需要删除此内容while。其中的代码只能运行一次。



 类似资料:
  • 这是我的代码: 该程序的目的是要求用户输入一个字符串,然后统计字符串中每个字符的使用次数。 当我去编译程序时,它工作正常。当我运行程序时,我可以在弹出框中输入字符串,但是在我提交字符串并按确定后,我得到一个错误,说 我不完全确定问题是什么或如何解决。

  • 这似乎微不足道,但也许我对向量的理解并不是它应该是的。我在这段代码上得到[java.lang.ArrayIndexOutOfBoundsException:Array index out of range:1]。错误发生在第3行。既然look是基于向量的大小,那么数组怎么会越界呢? 方法“getChangeSets()”返回一个列表。该列表最初是作为向量创建的。

  • 我的目标是创建一个新的实例,其中前2个元素是原始元素,之后是最后3个元素的中间值。例如,< code>[5,2,9,1,7,4,6,3,8]变成< code>[5,2,5,2,7,4,6,4,6]。 我解决这个问题的方法是创建2个新的数组列表,其中一个是从索引2开始的3个元素之间的子数组,具有排序,最后将新子数组的索引1放入我的新数组列表中。但是,这会遇到索引越界错误。

  • 我一直在四处寻找是否有任何东西可以帮助我,但我不太了解人们的回答,任何我所了解的似乎都不能解决问题!所以基本上正如标题所说,我得到了一个数组索引越界异常,但我不知道为什么。非常感谢任何帮助。 代码:

  • 在Java中构建汇编程序:我试图将用户的输入读入名为的。如果用户输入的指令与-array中的一个匹配,那么相应的操作码将被计算出来并输出到一个文本文件中。 但是,在输入指令并尝试添加另一条指令后,我得到了一个索引越界异常。 如果你们能帮忙,我会很感激的。

  • 我犯的错误 我的密码 我试图将数组的最后5个元素存储在temp 2中。那我就换了。有更简单的方法吗?将数组的前五个元素与后五个元素切换?你会如何用for循环切换它们?