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 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);

if (DNewCard ==1){
}
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 DNewCard;

Dealer1 = r.nextInt(13) +1;
Dealer2 = r.nextInt(13) +1;
Check(0, 0, 0, Dealer1, Dealer2, Dealer3, 0, DNewCard);
Dealer3 = r.nextInt(13) +1;
Check(0, 0, 0, Dealer1, Dealer2, Dealer3, 0, DNewCard);
}
}
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);
System.out.println("You have lost.");
}
System.out.println("You have won!");
}
System.out.println("It's a tie!");
}
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

YesNo();
}
}``````
4
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by jwenting

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.

Edited by NP-complete: n/a