Help with Java

Status
Not open for further replies.
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 :p so my coding practices are probably gonna be things you should NOT do haha

Lol you beat me to it, nice work :thumbsup:
Just a note. This

if (start.matches ("No") || start.matches("no"))

is the same as this

if (start.matches ("[Nn]o")

I'd just use start.equalsIgnoreCase("no"), it's not quite equivalent to yours but it would do the trick.
 
@SOULphIRE... I wouldn't even know where to start... I get confused :tongue:
@kmote... I had no idea about that. I need to read up more about this String thing... The book I'm using hasn't explained it yet :tongue:

EDIT: Alright.. I think I got it all down:

Code:
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.");
		}
	}
}

EDIT: Small fixes and updates...

Code:
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.");
		}
	}
}
I added more outputs for different W/L scenarios.
I'm not sure how efficient it is.. but it works :tongue:
 
totalUsr = card + card + totalUsr;
This will deal you two of the same card. Also don't need totalUsr on the end, because it'll just be 0.

if (start.equalsIgnoreCase("yes")){
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.

eh, I'll just do a quick rewrite and comment code changes this time :p

Code:
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.");
		
	}
}
 
Oh.. I didn't know that if there's a tie the dealer wins :tongue:
I stuck them into two massive if statements because it's more organized to me :tongue:

Well this has been really informative. Thanks for the help :thumbsup:

EDIT:

I added two more card1 = 0 and card2 = 0 for the first two cards drawn. The original "card" is being used for everything else now. :tongue:
EDIT2: oops, disregard that.. I edited the wrong thing :tongue:

EDIT3:
Code:
		if (start.equalsIgnoreCase("yes"))	{
			card1 = CardRand.nextInt(10)+1;
			card2 = CardRand.nextInt(10)+1;
			totalUsr =totalUsr + card1 + card2;

There... that's the right one!
 
edit: ah, that makes more sense lol. But the below point still stands.
Also, a for loop will be more resource friendly than two new variables. The one variable in the for loop will be released when the loop is exited.
Code:
for (int t=1; t<3; t++){
      card = CardRand.nextInt(10)+1;
      totalUsr += card;
}
that's the format.
int t declares your variable and sets starting value, t<3 is the field where you put the condition for the loop to keep running (in this case it'll exit if t is no longer < 3), t++ is incrementing by 1 every loop (or more if you want).
 
Fixed it :tongue: I need to learn more about this stuff... I need a new book :tongue:
Amazon usually has 'em for a good price... I'm going to buy a few more before summer is out.
 
Pah books are for sissies :p just think of something you wanna make and try and make it. You should be able to have a general idea of the process (i.e pseudocode) and just google on what to do for specific parts.

e.g. if you wanted to get a card value twice I'd straight away think of some kind of loop, then google 'java loop' and look through the different kinds of loops available. Lol that's actually what I did this time, like I said I'm no java guru I hardly know any functions. Just good at googling :grin:
 
Nice :tongue: I'll try it. That's sort of what I did with this 21 program. The others were from the book :tongue:
 
Status
Not open for further replies.
Back
Top Bottom