I'll take a look now. Bear in mind the last time I did Java was last year, decided to teach myself for two weeks lol then got distracted by Python again so my coding practices are probably gonna be things you should NOT do haha
Lol you beat me to it, nice work
Just a note. This
if (start.matches ("No") || start.matches("no"))
is the same as this
if (start.matches ("[Nn]o")
import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;
import static java.lang.System.in;
public class Play21 {
public static void main(String args[]) {
Random CardRand = new Random();
Scanner Keyboard = new Scanner(in);
String start, hit = "Yes";
int card = 0, totalUsr = 0, totalSys = 0, chk = 1;
// DEALER LOGIC STARTS
while(totalSys <= 16) {
card = CardRand.nextInt(10)+1;
totalSys += card;
}
// DEALER LOGIC ENDS
out.println("Would you like to play a game?");
out.print("Yes or No: "); start = Keyboard.nextLine();
// USER LOGIC STARTS
if (start.equalsIgnoreCase("yes")) {
card = CardRand.nextInt(10)+1;
totalUsr = card + card + totalUsr;
if (start.equalsIgnoreCase("yes")){
while (totalUsr < 21 && chk == 1) {
out.println("Your current card total is " + totalUsr + ".");
out.print("Do you want a hit? "); hit = Keyboard.nextLine();
out.println();
if (hit.equalsIgnoreCase("yes")){
card = CardRand.nextInt(10)+1;
totalUsr += card;
} else {
chk=0;
}
}
// USER LOGIC ENDS
// W/L/T LOGIC STARTS
if((totalUsr == 21 && totalSys !=21) || (totalSys > 21 && totalUsr <= 21 ) || (totalUsr > totalSys && totalUsr <= 21)) {
out.println();
out.println("You win! Your total: " + totalUsr);
out.println("The dealer's total: " + totalSys);
out.println();
} if((totalUsr != 21 && totalSys == 21) || (totalSys > totalUsr && totalUsr < 21 && totalSys < 21 )) {
out.println();
out.println("You lose! Your total: " + totalUsr);
out.println("The Dealer's total: " + totalSys);
out.println();
} if(totalUsr == totalSys && totalUsr <= 21) {
out.println();
out.println("It's a tie! Total: " + totalSys);
out.println();
}
// W/L/T LOGIC ENDS
} if(start.equalsIgnoreCase("no")) {
out.println("Oh, that's too bad.");
}
out.println("Thank you. Good bye.");
}
}
}
import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;
import static java.lang.System.in;
public class Play21 {
public static void main(String args[]) {
Random CardRand = new Random();
Scanner Keyboard = new Scanner(in);
String start, hit = "Yes";
int card = 0, totalUsr = 0, totalSys = 0, chk = 1;
// DEALER LOGIC STARTS
while(totalSys <= 16) {
card = CardRand.nextInt(10)+1;
totalSys += card;
}
// DEALER LOGIC ENDS
out.println("Would you like to play a game?");
out.print("Yes or No: "); start = Keyboard.nextLine();
// USER LOGIC STARTS
if (start.equalsIgnoreCase("yes")) {
card = CardRand.nextInt(10)+1;
totalUsr = card + card + totalUsr;
if (start.equalsIgnoreCase("yes")){
while (totalUsr < 21 && chk == 1) {
out.println("Your current card total is " + totalUsr + ".");
out.print("Do you want a hit? "); hit = Keyboard.nextLine();
out.println();
if (hit.equalsIgnoreCase("yes")){
card = CardRand.nextInt(10)+1;
totalUsr += card;
} else {
chk=0;
}
}
// USER LOGIC ENDS
// W/L/T LOGIC STARTS
if((totalUsr == 21 && totalSys !=21) || (totalSys > 21 && totalUsr <= 21 ) || (totalUsr > totalSys && totalUsr <= 21)) {
if (totalSys > 21 && totalUsr <= 21) {
out.println();
out.println("The dealer busted! You win!");
out.println("Your total: " + totalUsr + " Dealer total: " + totalSys);
out.println();
} if (totalUsr == 21 && totalSys !=21) {
out.println();
out.println("You win! You scored 21!");
out.println("Your total: " + totalUsr + " Dealer total: " + totalSys);
out.println();
} if (totalUsr > totalSys && totalUsr <= 21) {
out.println();
out.println("You win! Your total: " + totalUsr);
out.println("The dealer's total: " + totalSys);
out.println();
}
} if((totalUsr != 21 && totalSys == 21) || (totalSys > totalUsr && totalUsr < 21 && totalSys < 21 ) || (totalUsr > 21 && totalSys <= 21)) {
if (totalUsr != 21 && totalSys == 21) {
out.println();
out.println("The dealer scored a perfect 21... You lose!");
out.println("Your total: " + totalUsr + " Dealer total: " + totalSys);
out.println();
} if(totalSys > totalUsr && totalUsr < 21 && totalSys < 21 ) {
out.println();
out.println("The dealer scored higher. You lose!");
out.println("Your total: " + totalUsr + " Dealer total: " + totalSys);
out.println();
} if (totalUsr > 21 && totalSys <= 21) {
out.println();
out.println("You busted! You lose!");
out.println("Your total: " + totalUsr + " Dealer total: " + totalSys);
out.println();
}
} if(totalUsr == totalSys && totalUsr <= 21) {
out.println();
out.println("It's a tie! Total: " + totalSys);
out.println();
}
// W/L/T LOGIC ENDS
} if(start.equalsIgnoreCase("no")) {
out.println("Oh, that's too bad.");
}
out.println("Thank you. Good bye.");
}
}
}
This will deal you two of the same card. Also don't need totalUsr on the end, because it'll just be 0.totalUsr = card + card + totalUsr;
is already inside an 'if start.equals yes' statement, so you don't need that second if statement. Also the first statement includes the 'if start.equals no' statement, so it never gets accessed unless the end bracket for the first 'if yes' statement is moved.if (start.equalsIgnoreCase("yes")){
package play21;
import java.util.Scanner;
import java.util.Random;
import static java.lang.System.out;
import static java.lang.System.in;
public class Play21 {
public static void main(String args[]) {
Random CardRand = new Random();
Scanner Keyboard = new Scanner(in);
String start, hit = "Yes";
int card = 0, totalUsr = 0, totalSys = 0, chk = 1;
// DEALER LOGIC STARTS
while(totalSys <= 16) {
card = CardRand.nextInt(10)+1;
totalSys += card;
}
// DEALER LOGIC ENDS
out.println("Would you like to play a game?");
out.print("Yes or No: "); start = Keyboard.nextLine();
// USER LOGIC STARTS
if (start.equalsIgnoreCase("yes")) {
//Added for statement to get two different card values
for (int t=1; t<3; t++){
card = CardRand.nextInt(10)+1;
totalUsr += card;
out.println("Card " + t + " is " + card);
}
while (totalUsr < 21 && chk == 1) {
out.println("Your current card total is " + totalUsr + ".");
out.print("Do you want a hit? "); hit = Keyboard.nextLine();
out.println();
if (hit.equalsIgnoreCase("yes")){
card = CardRand.nextInt(10)+1;
totalUsr += card;
} else {
chk=0;
}
}
// USER LOGIC ENDS
// W/L LOGIC STARTS
// Got rid of the two massive if statements, all you're doing is checking twice, as the same if statements exist inside the big if statement already.
// Also cleaned up the if statements below, compare them and see what I changed.
// "Else if" means that if an "if" statment before it is already matched, it won't be checked.
// No such thing as a tie in blackjack, if equal then dealer wins.
if (totalSys > 21 && totalUsr < 21) {
out.println();
out.println("The dealer busted! You win!");
out.println("Your total: " + totalUsr + " Dealer total: " + totalSys);
out.println();
} else if (totalUsr == 21) {//think the rules are that if you get 21 in blackjack you get automatic payout.
out.println();
out.println("You win! You scored 21!");
out.println("Your total: " + totalUsr + " Dealer total: " + totalSys);
out.println();
} else if (totalUsr > totalSys && totalUsr < 21) {
out.println();
out.println("You win! Your total: " + totalUsr);
out.println("The dealer's total: " + totalSys);
out.println();
} else if (totalSys == 21) {//note we can change this now becase it's an else if statement. If totalUsr was 21 this wouldn't have been read.
out.println();
out.println("The dealer scored a perfect 21... You lose!");
out.println("Your total: " + totalUsr + " Dealer total: " + totalSys);
out.println();
} else if(totalSys >= totalUsr && totalUsr < 21) {
out.println();
out.println("The dealer wins. You lose!");
out.println("Your total: " + totalUsr + " Dealer total: " + totalSys);
out.println();
} else if (totalUsr > 21 && totalSys < 21) {//again, don't need <= for totalSys here becase of the "else if toatlSys == 21" statement before.
out.println();
out.println("You busted! You lose!");
out.println("Your total: " + totalUsr + " Dealer total: " + totalSys);
out.println();
}
// W/L LOGIC ENDS
} if(start.equalsIgnoreCase("no")) {
out.println("Oh, that's too bad.");
}
out.println("Thank you. Good bye.");
}
}
if (start.equalsIgnoreCase("yes")) {
card1 = CardRand.nextInt(10)+1;
card2 = CardRand.nextInt(10)+1;
totalUsr =totalUsr + card1 + card2;
for (int t=1; t<3; t++){
card = CardRand.nextInt(10)+1;
totalUsr += card;
}