Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:26:31+00:00 2026-05-28T18:26:31+00:00

So I’m working on a Black jack program and I’m a little stuck. I’ll

  • 0

So I’m working on a Black jack program and I’m a little stuck. I’ll warn everyone that I’m really new to programming, also, I’m mid-project….so there are some loose ends and unused variables, and some unnecessary logic (for testing) but here’s whats I need help with.

1) I’m using Math.random to act as the deck of cards, and at first it seemed to work okay…but after the second hit, it’s clear that the previous card value is replaced with the current card value. if you drew a 3,5,9…the array would read 9,9,9 instead of 3,5,9.

2) Eclipse has indicated that the variables user_total (in method user_hand), and dealer_total(in method dealer_hand), can not be returned to main because they “cannot be resolved to a variable”. I don’t understand why, they’re normal int’s as far as I can tell.

sorry if it’s formatted weird, stackoverflow was complaining about something…
here’s my code:

public class blackJack 
{
final static int DEALER_STAND_THRSH = 17;
final static int MAX_CARD_VALUE = 10;
static Random randomNumbers = new Random();

public static void main(String[] args)throws IOException
{
BufferedReader in;
in = new BufferedReader (new InputStreamReader (System.in));
int number_of_hits=0;
boolean still_playing=true;
while (still_playing)
{   
//tracks number of hits----------------------------------

number_of_hits ++;
System.out.println("this is draw  : " + number_of_hits);
System.out.println(" ");

// gets users 
int user_total=user_hand(number_of_hits);
//get dealers card----------------------------------------
int dealer_total=dealer_hand(number_of_hits);
//ask user if they'd like to hit or stand
System.out.println("\nwould you like to hit or stand?\nenter:H to hit\nenter:S to stand");
char hit_or_stand=in.readLine().charAt(0);
char hit_or_stand_case = Character.toLowerCase(hit_or_stand);

if (hit_or_stand_case=='s')
{               
//compare cards
who_wins(user_total,dealer_total );
}

// continue game prompt --------------------------------
System.out.println("are you still playing? enter 1 for yes. 2 for no.");
int still_playing_response = Integer.parseInt(in.readLine());

if(still_playing_response==1)
{
still_playing=true;
}
else
{
still_playing=true;
System.out.println("goodbye");
}
}//while end
}//main end

public static int generate_a_card()
{

Scanner input = new Scanner(System.in);

int card=randomNumbers.nextInt(MAX_CARD_VALUE) +1 ;

return card;

}

public static int user_hand(int number_of_hits)
{   
int user_card=generate_a_card();
System.out.println( "you drew: " + user_card );
int user_current_hand [] = new int [number_of_hits];

for (int i=0; i<user_current_hand.length; i++)
{
user_current_hand [i]= user_card;
System.out.println( user_card + " has been stored in index " + i + " of user_current_hand array");
int user_total=0;

for(int j=0; j<user_current_hand.length; j++)
{
user_total+= user_current_hand[i];

if (user_total>21)
{
System.out.println("you have exeeded 21, you lose!");
}//if end
}//nested for loop

}//first for loop


return user_total;


}//end user_hand method

public static int dealer_hand(int number_of_hits )

{       
System.out.println("-----------------------------------------\n");
System.out.println("now for the dealers draw");
System.out.println(" ");

int dealer_card=generate_a_card();
System.out.println( "the dealer drew: " + dealer_card);
int dealer_current_hand [] = new int [number_of_hits];

for (int i=0; i<dealer_current_hand.length; i++)
{
dealer_current_hand [i]= dealer_card;
System.out.println( dealer_card + " has been stored in index " + i + " dealer_current_hand array");

int dealer_total=0;
for(int j=0; j<dealer_current_hand.length; j++)
{
dealer_total+= dealer_current_hand[i];

if (dealer_total>21)
{
System.out.println("the dealer has exeeded 21, you win!");
}//if end
}//nested for loop
}//first for loop

return dealer_total;

}//end main

public static void who_wins(int user_total, int dealer_total  )
{

if(user_total > dealer_total)
{
System.out.println("congradulations, you won!\nYour score: " + user_total + "\ncomputer's score: " + dealer_total);
}
else if(user_total < dealer_total)
{
System.out.println("Sorry, you lost.\nYour score: " + user_total + "\ncomputer's score: " + dealer_total);
}
else
{
System.out.println("this round was a tie!\nYour score: " + user_total + "\ncomputer's score: " + dealer_total);
}

}


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:26:34+00:00Added an answer on May 28, 2026 at 6:26 pm

    user_total only access on within that loop. You return user_total on out side the loop. Because of that Eclipse can’t identify that variable. Because variable is out of the scope.

    declare this way,

    public static int user_hand(int number_of_hits)
    {   
         int user_total=0;
         int user_card=generate_a_card();
         System.out.println( "you drew: " + user_card );
         int user_current_hand [] = new int [number_of_hits];
    
         for (int i=0; i<user_current_hand.length; i++)
         {
              user_current_hand [i]= user_card;
              System.out.println( user_card + " has been stored in index " + i + " of 
                    user_current_hand array");
    
    
               for(int j=0; j<user_current_hand.length; j++)
                  {
               user_total+= user_current_hand[i];
    
                 if (user_total>21)
                {
                    System.out.println("you have exeeded 21, you lose!");
                 }//if end
           }//nested for loop
    
        }//first for loop
    
    
       return user_total;
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.