So I made a blackjack code in java and I need to find a way to replace the place where I added a system.exit with a way to ask the user if they'd like to play again and restart the loop, keep in mind that I don't need the program to restart as I'd like to keep the value of their chips considering if they've won or lost. Can anybody help me?
Secondly, because it is a blackjack code, when it deals the cards, I would like for it to also print out K, Q or J but still consider it the number 11. Any ideas as to how I can do that?
Can I make an Ace count as 1 and 11?
Thanks so much, please remember, I am a very amateur programmer. I have just started coding so my knowledge is very limited. I am completely willing to learn however. Thanks so much and all input is appreciated.
One last question, is there anyway to add the suits of the cards such as (clubs, spades etc.) but the actual signs and if the signs aren't possible at all then the letter ('C', 'S', 'H', 'J') will have to do I guess. Thanks again, looking forward to the responses.
import java.util.Scanner; class Blackjack { public static void main(String[] args) { Scanner scan = new Scanner (System.in); Scanner num = new Scanner (System.in); System.out.println("Welcome to Blackjack!"); System.out.println("Here are 500 chips, enjoy your stay!"); int rndmdel, rndmusercard1, rndmusercard2, rndmusersum, cardsuit=0, cardcount=1, counter=0, chips =500, bet=0; String userchoice = "hit"; String tryagain = ""; while (chips >0) { userchoice = "hit"; rndmusersum=0; rndmdel = (int)((11-1+1)*Math.random()+1); rndmusercard1 = (int)((11-1+1)*Math.random()+1); rndmusercard2 = (int)((10-1+1)*Math.random()+1); System.out.println("How many chips would you like to bet?"); bet = num.nextInt(); if (bet/chips ==1 ) { System.out.println("All IN!"); }else if (bet >= (chips/2)) { System.out.println("Oooooh! Big Baller!"); }else if (bet <= (chips/10)) { System.out.println("Oh c'mon! Live a little, will ya?"); }try { Thread.sleep(2000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } System.out.println("The dealer deals the cards!"); try { Thread.sleep(2000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } System.out.println("The dealer's first card is " + rndmdel); try { Thread.sleep(2000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } System.out.println("Your first card is "+ rndmusercard1); try { Thread.sleep(2000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } System.out.println("Your second card is "+rndmusercard2); try { Thread.sleep(2000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } rndmusersum = rndmusercard1+rndmusercard2; System.out.println("The sum of your cards is " + rndmusersum); while (rndmusersum < 21 && userchoice.equalsIgnoreCase("hit")) { System.out.println("Would you like to hit or stay?"); userchoice = scan.nextLine(); if (userchoice.equalsIgnoreCase("hit")) { rndmusercard1 = (int)((11-1+1)*Math.random()+1); rndmusersum = rndmusersum + rndmusercard1; System.out.println("HIT ME!....."); try { Thread.sleep(400); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } System.out.println(rndmusersum); }else { System.out.println(".......staaay!"); System.out.println("You decided to stay at " +rndmusersum); } if (rndmusersum == 21) { System.out.println("Congratualtions! You win!"); System.out.println("You just won " + bet + " chips!"); chips = chips + bet; System.out.println("You now have " + chips + " chips!"); try { Thread.sleep(2000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } System.exit(0); } else if (rndmusersum > 21) { System.out.println("BUSTED! Sorry you lose!"); System.out.println("Unfortunately you lost " + bet + " chips!"); chips = chips - bet; System.out.println("You now have " + chips + " chips!"); try { Thread.sleep(2000); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } System.exit(0); } } System.out.println("The dealer's first card was " +rndmdel); while (rndmdel <= rndmusersum) { rndmusercard1 = (int)((11-1+1)*Math.random()+1); rndmdel += rndmusercard1; System.out.println("."); try { Thread.sleep(200); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } System.out.println("..."); try { Thread.sleep(500); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } System.out.println("...... HIT!"); try { Thread.sleep(900); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } System.out.println("The dealer gets a " +rndmusercard1); System.out.println("The dealer's total is " + rndmdel); } if (rndmdel > 21) { System.out.println("The dealer BUSTS! Congratuations! You win!"); System.out.println("You just won " + bet + " chips!"); chips = chips + bet; System.out.println("You now have " + chips + " chips!"); } else if (rndmdel <= 21 & rndmdel > rndmusersum) { System.out.println("Sorry, the dealer wins this round"); System.out.println("Unfortunately you lost " + bet + " chips!"); chips = chips - bet; if (chips == 0) { System.out.println("All out of chips huh?!? Sorry, come back again later"); } else { System.out.println("You now have " + chips + " chips!"); } } } } } //replace system.exits with a keeping playing option // add K J and Q and A (1 and 11) // incorporate arrays, methods and printstreams.