473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How do I add random letter in java (for blackjack game)

I'm fairly new to java and need help with adding letters (J, Q, K, A) into the program and adding values for each. Thanks.

Expand|Select|Wrap|Line Numbers
  1. // February 8, 2008
  2. // The "BlackJack" class.
  3. import java.awt.*;
  4. import hsa.Console;
  5. import java.util.Random; // Only the Random class
  6. import java.util.*;      // All classes in the java.util package
  7.  
  8. public class BlackJack
  9. {
  10.     static Console c;           // The output console
  11.  
  12.     public static void main (String[] args)
  13.     {
  14.         c = new Console ();
  15.  
  16.         char another;   
  17.         c.println("******************");
  18.         c.println("B-L-A-C-K-J-A-C-K");
  19.         c.println("******************");
  20.         c.println ("Your Cards:");
  21.         int n = (int)(10.0 * Math.random()) + 1;
  22.         int m = (int)(10.0 * Math.random()) + 1;
  23.         c.print ("|" + n);
  24.         c.println ("| |" + m + "|");
  25.         int total;
  26.         total = n+m;
  27.         c.println ("You now have " + total);
  28.         if (total <=21)
  29.         {
  30.         c.println ();
  31.         c.print ("Do you want another? (y/n) ");
  32.         another = c.readChar();
  33.                 if (another == 'y' | another == 'Y')
  34.         {
  35.                 int a = (int)(10.0 * Math.random()) + 1;
  36.                 c.println ("You have added the card " + a);
  37.                 c.println ("You now have in total " + (n+m+a));
  38.         }
  39.         else if (total >21)
  40.         {
  41.         c.println ();
  42.         c.print ("Your busted!");
  43.         }
  44.         }
  45.     } // main method
  46. } // BlackJack class
  47.  
Feb 9 '08 #1
7 5991
Laharl
849 Expert 512MB
Make an array of characters and fill it with 0-9, J, Q, K, A. Then, use Random to generate which index you need and that's your card.
Feb 9 '08 #2
Please include the code of that, I dont know what arrays are.
Feb 9 '08 #3
Laharl
849 Expert 512MB
Sun's Java Tutorial can explain them to you far better than I.
Feb 9 '08 #4
I think what you are trying to do is have the program print out a letter instead of the numbers 11, 12, 13, & 1 Right?
I am assuming you are printing the numbers to the screen right.
if so make a Switch after your random number but befor you display

switch (your number variable)
{
case 1: System.printline("A"); break;
case 11: System.printline("J"); break;
case 12: System.printline("Q"); break;
case 13: System.printline("K"); break;
}

Just have your program remember the numbers. Player only has to see the cards and nothing else.

Hopefully this helps.


Part I forgot:
Also you will want to keep track of the numbers seperately so you can always print out the right letters. you can Make a Case 14. Just make sure that when you reference the array you have your method say something like
(If Intname == 13) total += 10;
same for 11, and 12. That way when they have a face card you aren't giving them more then what it is actually worth.
Feb 10 '08 #5
how do say do you want another card?
Mar 20 '10 #6
pbrockway2
151 Expert 100+
@ukrainehigh
Rather than resurrect an old thread you might do better to start your own. It's also best to ask the question as precisely as possible - usually with your own code and a statement about what is going wrong (compiler message or a description of the runtime behaviour.)
Mar 20 '10 #7
I dont want to put it up in public since it's going to get marked soon is there anyway to pm you?
Mar 23 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: M. Braun | last post by:
Hello, first I have to say, my english isn't very well so just try to understand it ;-). I've got a little problem with the PHP function rand(). I want to create a little programm which...
23
by: Thomas Mlynarczyk | last post by:
I remember there is a programming language where you can initialize the random number generator, so that it can - if you want - give you the exactly same sequence of random numbers every time you...
3
by: slyphiad | last post by:
Here's the problem that i got... I'm trying to create a blackjack game. Here, I'm trying to create 2 blackjack games. A game with bet and without bet. So basically what i did, was create 2...
7
by: Micheal Artindale | last post by:
I am looking at creating list of letter combinations. letters a-h 6 letters per combination letter can repeat its self in the combination, but not next to its self and, a series of letter can...
4
by: darrel | last post by:
I can grab a random number in vb.net like this: Dim RandomClass As New Random Dim RandomNumber As Integer RandomNumber = RandomClass.Next(1, 26) However, what I want is a random number. Short...
4
by: eltower | last post by:
Hey all, I'm trying to write a program in Python for learning purposes which is meant to: Generate a random number from 0 to 6 Insert this random number to the end of a list unless the number...
3
by: devilinthebox | last post by:
I am not really familar with Java and I need help with creating this simple Blackjack program. Here is a layout of how the program should output: If the computer has more than 16 it wins,...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
9
by: cdm2883 | last post by:
Ok im trying to make a lil random number game. I have most of the code and comments on how i want the game to work. but i can not get to it work. if you have any answers any help what so ever it woul...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.