I'm trying to create a blackjack program for school.
No matter what I've been doing, this keeps coming with errors.
My teacher says theres some logic errors, and i don't understand it.

import java.util.Scanner;
 import java.util.Random;
 
public class BlackJack {
	public static void YesNo(){
		Scanner input = new Scanner(System.in);
    	int YesNo;
    	
    	System.out.println("Would you like to begin?");
    	System.out.println("1 for yes, 2 for no.");
    	YesNo = input.nextInt();
    	
    	if (YesNo == 1){
    		System.out.println("If you get a total below 16, you will be able to acuire a third card. Ace is either 1 or 11.");
    		System.out.println("We will now begin.");
    	}
    	while (YesNo == 2){
    	    System.out.println("It will hopefully crash now.");
    	}
    	GameStart();
    }

	public static void Check(int Card1, int Card2, int Card3, int Dealer1, int Dealer2, int Dealer3, int NewCard, int DNewCard){
	
		int CardTotal, DealerTotal;
		int CValue1, CValue2, CValue3;
		int DValue1, DValue2, DValue3;
		
		if (Card1 >= 11){
			CValue1 = (10);
			}		
		if (Card1 < 11){
			CValue1=Card1;
		}
		if (Card2 >= 11){
			CValue2 = (10);
			}	
		if (Card2 < 11){
			CValue2=Card2;
		}	
		if (NewCard == 1){
			if (Card3 >= 11){
				CValue3 = (10);
			}			
		if (Card3 < 11){
			CValue3=Card3;
		}
		}
		
		if (Dealer1 >= 11){
			DValue1 = (10);
		}
		if (Dealer1 < 11){
			DValue1= Dealer1;
		}
		if (Dealer2 >= 11){
			DValue2 = (10);
		}
		if (Dealer2 < 11){
			DValue2= Dealer2;
		}
		if (DNewCard ==1){
			if (Dealer3 >= 11){
				DValue3 = (10);
			}
		if (Dealer3 < 11){
			DValue3= Dealer3;
		}
		}
		
		CardTotal = (CValue1+CValue2);
		DealerTotal = (DValue1+DValue2);
		
		if (DNewCard ==1){
			DealerTotal = (DealerTotal+DValue3);
		}
		if (NewCard ==1){
			CardTotal = (CardTotal+CValue3);
		}
}	
	public static void DealerCards(int CardTotal){
	
		Scanner input = new Scanner(System.in);
		Random r = new Random();
		int Dealer1, Dealer2, Dealer3;
		int DValue1, DValue2, DValue3;
		int DealerTotal;
		int DNewCard;
		
		Dealer1 = r.nextInt(13) +1;
		Dealer2 = r.nextInt(13) +1;
		Check(0, 0, 0, Dealer1, Dealer2, Dealer3, 0, DNewCard);
		if(DealerTotal <= 16){
			Dealer3 = r.nextInt(13) +1;
			Check(0, 0, 0, Dealer1, Dealer2, Dealer3, 0, DNewCard);
		}
		Winner(CardTotal, DealerTotal);
}
    public static void GameStart(){
    
    	Scanner input = new Scanner(System.in);

    	Random r = new Random();
    	int Card1, Card2, Card3;
    	int CValue1, CValue2, CValue3;
    	int CardTotal;
    	int NewCard;
    	
    	Card1 = r.nextInt(13) + 1;
    	Card2 = r.nextInt(13) + 1;
    	Check(Card1, Card2, Card3, 0, 0, 0, NewCard, 0);
    	System.out.println("Your cards are" + Card1 +" and " + Card2 +" and the total is "+CardTotal);
    	if (CardTotal <= 16){
    		System.out.println("Your total is 16 or under, would you like a second card?");
    		System.out.println("1 for yes, 2 for no.");
    		NewCard = input.nextInt();
    		
    		if (NewCard == 1){
    			Card3 = r.nextInt(13) +1;
    			Check(Card1, Card2, Card3, 0, 0, 0, NewCard, 0);
    			System.out.println("Your new card is " +Card3+" and you new total is "+CardTotal);
    		}
    	}
    	
    	DealerCards(CardTotal);
    }
    public static void Winner(int CardTotal, int DealerTotal) {
    	
    	System.out.println("The dealer has " +DealerTotal+" and you have "+CardTotal);
    	if (DealerTotal > CardTotal){
    		System.out.println("You have lost.");
    	}
    	if (CardTotal > DealerTotal){
    		System.out.println("You have won!");
    	}
    	if (DealerTotal == CardTotal){
    		System.out.println("It's a tie!");
    	}
    }
    
    public static void main(String[] args) {
    	Scanner input = new Scanner(System.in);
    	
		YesNo();
    }
}

Recommended Answers

All 7 Replies

can you just elaborate more where you are finding the logic error..?

I have no idea. It's just that my teacher said I have some.
And I think there is also something else wrong with it...

well, it seems you are out of scope with some variables, particularly the cardTotal variable. For e.g. the cardTotal you declared in line no. 25 is not the same as the one in line no. 106.
The one declared in the Check method dies out as you go out of the method and hence the value is not available inside the GameStart method. Hence the logic checking in line no 113 makes no sense.

Hope that helps.

same problem with dealerTotal.

well to give you a workaround...you can use two different methods one for cards and one for dealers and make them each "return" their respective totals.

@NP-complete, what you said would have helped me, if I understood what it meant.

Excuse me for my lack of knowledge with programming terms and stuff. x-x

@Dok, Thanks, that helps a lot. But that doesn't explain to me how to fix this program. =\

you "fix" it by analysing the observed incorrect behaviour and from that deducing what in your code is responsible for that behaviour, then correcting that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.