# Blackjack

• November 27th, 2013, 09:57 AM
gavinqotsa92
Blackjack
I'm working on a program to simulate a game of blackjack and I'm a bit stuck. I want to have a running score that would be "score + value" after each card is dealt, I know I'm going the wrong way about it but just can't figure it out, any help is greatly appreciated!

import java.util.Scanner;
import java.util.Random;

class blackjack{
public static void main(String args[]){

Scanner in = new Scanner(System.in);

int value, score=0;
String card1, card2, card3, card4, card5, play1, play2, play3;

Random random = new Random();

String[] suit = {"Spades", "Hearts", "Clubs", "Diamonds"};
int selectS = random.nextInt(suit.length);

String[] rank = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
int selectR = random.nextInt(rank.length);

card1 = (rank[selectR] + " of " + suit[selectS]);
card2 = (rank[selectR] + " of " + suit[selectS]);
card3 = (rank[selectR] + " of " + suit[selectS]);
card4 = (rank[selectR] + " of " + suit[selectS]);
card5 = (rank[selectR] + " of " + suit[selectS]);

if(selectR >=9 ) {
value = 10;
}

if(selectR ==0 && score < 22) {
value = 11;
}

if(selectR ==0 && score > 21){
value = 1;
}

if(selectR >0 && selectR<9 ) {
value = selectR+1;
}

System.out.println(card1);
System.out.println(card2+"\nRunning score : "+score);

System.out.print("\nHit or stay?");
play1 = in.nextLine();
if(play1.equals("hit")){
System.out.println(card3+"\nRunning score : "+score);
}

System.out.print("\nHit or stay?");
play2 = in.nextLine();
if(play2.equals("hit")){
System.out.println(card4+"\nRunning score : "+score);
}

System.out.print("\nHit or stay?");
play3 = in.nextLine();
if(play3.equals("hit")){
System.out.println(card5+"\nRunning score : "+score);
}

} //end main
} //end class
• November 27th, 2013, 10:25 AM
KevinWorkman
Re: Blackjack
When posting code, please use the highlight tags and standard naming conventions.

I don't see any place in this code where you set or increment the score variable.

Also, have you not learned about loops yet?
• November 28th, 2013, 07:46 AM
gavinqotsa92
Re: Blackjack
I just learned about loops today and got it working, thanks for the help!