Reputation: 57
I am building a Blackjack game using Object Oriented Programming.
I wrote this so far:
import java.util.Random;
public class Blackjack {
public static String[] Card = new String[12];
public static String[] Suits = {"D", "H", "S", "C"};
public void setCard(String[] c) {
Card = c;
Card[0] = "A";
Card[1] = "2";
Card[2] = "3";
Card[3] = "4";
Card[4] = "5";
Card[5] = "6";
Card[6] = "7";
Card[7] = "8";
Card[8] = "9";
Card[9] = "10";
Card[10] = "J";
Card[11] = "Q";
Card[12] = "K";
}
public static void main(String[] args) {
System.out.println("Welcome to Blackjack.");
Random rand = new Random();
int card_Sign1 = (int) (Math.random() * 13);
int card_Sign2 = (int) (Math.random() * 13);
int Suit1 = (int) (Math.random() * 4);
int Suit2 = (int) (Math.random() * 4);
System.out.print(Card[card_Sign1]);
}
}
The output I get is Welcome to Blackjack
then null
. I was wondering if I could get a little help on how to solve this.
Upvotes: 0
Views: 2310
Reputation: 19
You forgot the call to setCard()
and you don't need to pass parameters to setCard()
. Try this:
public static void main(String [] args) {
System.out.println("Welcome to Blackjack.");
Random rand = new Random();
int card_Sign1 = (int)(Math.random()*13);
int card_Sign2 = (int)(Math.random()*13);
int Suit1 = (int)(Math.random()*4);
int Suit2 = (int)(Math.random()*4);
setCard();
System.out.print(Card[card_Sign1]);
}
public void setCard() {
Card[0] = "A";
Card[1] = "2";
Card[2] = "3";
Card[3] = "4";
Card[4] = "5";
Card[5] = "6";
Card[6] = "7";
Card[7] = "8";
Card[8] = "9";
Card[9] = "10";
Card[10] = "J";
Card[11] = "Q";
Card[12] = "K";
}
Upvotes: 1
Reputation: 317
As csmckelvey said in a comment, you need to call the setCard method inside your main method. Otherwise, the card array never gets its initial values.
You have another problem as well--You declare your array with a size of 12:
public static String[] Card = new String [12];
But then you try to assign it 13 values in setCard. You'll need to change it to have a size of 13.
Also, setCard() doesn't need a paramater, you're fine just setting the values for Card.
Aside from that, looking good, keep it up!
Upvotes: 2