The aim of this program is to create a random number generator, and select cards numbered 1-11 (11 being the Ace). The game rules are not as important as the finished working program, I do not need suits, money or hi-score tables.
I have attempted and failed to create a card generator that will, produce a random card value each time it is called for. As you will see in the code, the int card1 = 1 +r.nextInt(); returns the same value every time. If it calls a 4 for card1, all future card1(s) will also be a 4. This is not what I had in mind when creating the int value 1-11 inc. I had hoped the card1 would return a random value each time.
Next I attempted and failed to add the card total and the newCard1 to a current int. This also does not work as intended.
Here is the code I have attempted so far, and please remember when your explaining your corrections to me, I am a first year student, and have little to none basic knowledge:
import java.util.Random;
import java.util.Scanner;
class blackj
{
public static void main(String[] args)
{
Random r = new Random();
Scanner keyboard = new Scanner(System.in);
String name;
boolean playing = true;
boolean notPlaying = true;
int card1 = 1 + r.nextInt(11);
int card2 = 1 + r.nextInt(11);
int dcard1 = 1 + r.nextInt(11);
int dcard2 = 1 + r.nextInt(11);
int ptotal = card1+card2;
int dtotal = dcard1+dcard2;
int pcurrent = ptotal+card1;
int dcurrent =dtotal+dcard1;
{
System.out.println("Welcome to Blackjack ! " );
System.out.println("Score as close to 21 without going over to win ");
System.out.println("What is your name?");
name = keyboard.nextLine();
System.out.println("Hello " + name);
System.out.println("Let's play some BlackJack!");
System.out.println("The dealer shows: \n\t\t" +dcard1 );
System.out.println("Your first card is: \n\t\t " +card1 );
System.out.println("Your second card is: \n\t\t" +card2 );
System.out.println("Giving you a grand total of: " +ptotal );
while (playing)
{
System.out.println("Would you like to (H)it or (S)tick?");
Scanner hit1 = new Scanner(System.in);
String a = hit1.nextLine();
if(a.equals("s"))
{
System.out.println("You stick at " );
System.out.println("Now it's the dealers turn\n Dealer must draw until 17");
}
if(a.equals("h"))
{
System.out.println("Your next card is " +card2 );
System.out.println("Giving you a new total of "+pcurrent );
if ((pcurrent >=22))
System.out.println("You Busted! \nSorry! you lose");
}
else
{
System.out.println("Please press H or S");
}
}
}
}
}
As you can see I haven't gotten to the dealers part yet.