简体   繁体   中英

assign value to new variable?

My code goes like this:

import java.util.ArrayList;

import java.util.Scanner;

import java.util.Random;

/**
 *
 * @author Matt
 */
public class Deck {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Random r = new Random();
    String[][] DeckOfCards = new String[13][4];
    int i;
    int j;
    int k;
    String suit[] = new String[4];
    String number[] = new String[13];
    ArrayList draw = new ArrayList();
                                            //setting up the deck
    suit[0]="Diamonds, ";
    suit[1]="Clubs, ";
    suit[2]="Hearts, ";
    suit[3]="Spades, ";

    number[0]="Ace of ";
    number[1]="Two of ";
    number[2]="Three of ";
    number[3]="Four of ";
    number[4]="Five of ";
    number[5]="Six of ";
    number[6]="Seven of ";
    number[7]="Eight of ";
    number[8]="Nine of ";
    number[9]="Ten of ";
    number[10]="Jack of ";
    number[11]="Queen of ";
    number[12]="King of ";

                                    //dealing the initial hand

    for(j=0;j<3;j++){

        for(i=0;i<12;i++){

            DeckOfCards[i][j]=""+number[i]+suit[j];
            draw.add(DeckOfCards[i][j]);
            //System.out.println(DeckOfCards[i][j]);
        }
        //System.out.println("");
       // System.out.println("");
    }

    ArrayList drawn = new ArrayList();
    ArrayList hand = new ArrayList();
    int u;


    for(u=1;u<6;u++){   
        k=r.nextInt(52);

        if(!drawn.contains(k)){    

            //System.out.println(draw.get(k));
            hand.add(u+" "+draw.get(k).toString());
            drawn.add(k);

            }
        else{
               u--;
            }

              }
            System.out.println(hand.toString());

but the last if statement:

            if(!drawn.contains(k)){
            hand.add(u+" "+draw.get(k).toString());
            drawn.add(k);)

always stops the compiling say that I need to assign the new variable a return value? k is already assigned a value, before the if statement. It seems to work when I put the random k statement inside the if statement, but that makes it worthless, no? No errors in the editor itself but when I compile, it gives me this:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 41, Size: 36 at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.get(ArrayList.java:411) at deck.Deck.main(Deck.java:74) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

I'm completely out of ideas on how to solve this.

There are not enough cards in your deck. You are getting and index out bounds because you are trying to access draw.get(41) which doesnt exist because draw is only 36 in size. This is because your initialization of draw is incorrect. Your buggy code is:

for(j=0;j<3;j++){

        for(i=0;i<12;i++){

            DeckOfCards[i][j]=""+number[i]+suit[j];
            draw.add(DeckOfCards[i][j]);
            //System.out.println(DeckOfCards[i][j]);
        }
        //System.out.println("");
       // System.out.println("");
    }

in the above only adds 3 suits, 12 of each suit. (36 cards). You should be adding 4 suits, 13 of each suit. Perhaps you meant to say less than OR EQUAL TOO for (j=0; j<=3; j++) instead of just LESS THAN.

Your problem is that you are trying to draw one of 52 random cards, but you only have 36 cards in your deck:

for(j=0;j<3;j++){  // should be j<4

    for(i=0;i<12;i++){  // should be i<13

        DeckOfCards[i][j]=""+number[i]+suit[j];
        draw.add(DeckOfCards[i][j]);
        //System.out.println(DeckOfCards[i][j]);
    }
    //System.out.println("");
   // System.out.println("");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM