I'm just finishing off a project and I have a question about how to form the conditions of an if statement.
if(dscore >=pscore && <21 )
{
System.out.println("\nDealer has " +dscore);
System.out.println("\nThe dealer beat you!");
System.out.println("\nThe House Wins!");
dealersTurn = false;break;
}
I would like the if statement to be met:
if the dealers score is greater than the players score, but also less than 21.
Obviously it won't compile like that, I don't know the correct way to phrase the statement so it becomes a valid argument.
I'll attach the entire code, some of you may recognize from previous questions, the community has been helpful so far, and I'm grateful for the assistance in learning and doing java, my first year of study was going terribly until I came here.
Reading back over previous blackjack builds, mine is very simple, but almost completed. I got help working out the loops, and the if statements outside of the braces, rather than inside. Overall it's been a great experience learning here. So Thanks, to everyone.
The problem is when the dealer busts, his score is still higher than the players, but over the limit. I need a method to correct that.
import java.util.Random;
import java.util.Scanner;
class Blackjack
{
public static void main(String[] args)
{
Random r = new Random();
String name;
Scanner scannerIn = new Scanner(System.in);
boolean yourTurn = true;
boolean dealersTurn =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 pscore = card1 +card2;
int dscore = dcard1 +dcard2;
System.out.println("Welcome to Blackjack ! " );
System.out.println("\nScore as close to 21 without going over to win ");
System.out.println("\nWhat is your name?");
name = scannerIn.nextLine();
System.out.println("\nHello " + name);
System.out.println("\nLet's play some BlackJack!");
System.out.println("\nThe dealer shows:\t" +dcard1 );
System.out.println("\n\nYour first card is:\t " +card1 );
System.out.println("\nYour second card is:\t" +card2 );
System.out.println("\nGiving you a grand total of: " +pscore );
while (yourTurn)
{
if ((pscore <= +card1 +card2))
System.out.println("\nWould you like to (H)it or (S)tick?");
String a = scannerIn.nextLine();
if(a.toLowerCase().equals("h"))
{
int newCard = 1 + r.nextInt(11);
System.out.println("\nYour next card is " +newCard );
pscore = pscore +newCard;
System.out.println("\nGiving you a new total of "+pscore);
if ((pscore >=22))
{
System.out.println("\nYou Busted! \nSorry! you lose");
yourTurn = false;break;
}
}
else if(a.toLowerCase().equals("s"))
{
yourTurn = false;
System.out.println("\nYou stick at " +pscore );
System.out.println("\nNow it's the dealers turn\n Dealer must draw until 17");
}
else
{
System.out.println("\nPlease press H or S");
}
}
while (dealersTurn)
{
dealersTurn = true;
{ if ((dscore == dcard1+dcard2))
System.out.println("\nThe dealers cards were:\n " +dcard1);
System.out.println("\nand\n" +dcard2);
System.out.println("\nGiving the Dealer a grand total of: " +dscore );
}
if ((dscore<pscore))
{
int newCard1 = 1 + r.nextInt(11);
System.out.println("\nThe dealer draws a: " +newCard1 );
dscore = dscore +newCard1;
System.out.println("\nGiving the dealer a grand total of: "+dscore);
}
if (dscore<17)
{
int newCard2 = 1 + r.nextInt(11);
System.out.println("\nThe dealer draws a: " +newCard2 );
dscore = dscore +newCard2;
System.out.println("\nGiving the dealer a grand total of: "+dscore);
}
if (dscore==pscore)
{
System.out.println("Dealer matches your score and sticks at: " +dscore);
System.out.println("The House Wins\nYou failed to beat the dealer\nYou Lose! ");
dealersTurn = false;
}
if (dscore >=22)
{
System.out.println("\nDealer Bust!");
System.out.println("\nThe House loses");
System.out.println("\nYou Win");
dealersTurn = false;
}
if(dscore >=pscore & <21 )
{
System.out.println("\nDealer has " +dscore);
System.out.println("\nThe dealer beat you!");
System.out.println("\nThe House Wins!");
dealersTurn = false;break;
}
}
scannerIn.close();
}
}
Aucun commentaire:
Enregistrer un commentaire