473,416 Members | 1,698 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,416 software developers and data experts.

how to pick a random suit/rank from an array?

hi

I'm not that good at Java so I'm stuck

I got this array and I want to make the computer pick a random suit and rank from the list

I'm not sure if i can do that in the same class or just make a new

this is my array:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Card 
  3. {
  4.  
  5.     private String rank;
  6.     private String suit;
  7.  
  8.  
  9.     public Card(int rank, int suit)
  10.     {
  11.         if(suit == 0)
  12.         {
  13.             this.suit = "Spades";
  14.         }
  15.         else if(suit == 1)
  16.         {
  17.             this.suit = "Diamonds";
  18.         }
  19.         else if(suit == 2)
  20.         {
  21.             this.suit = "Clubs";
  22.         }
  23.          else if(suit == 3)
  24.         {
  25.             this.suit = "Hearts";
  26.         }
  27.         else
  28.         {
  29.         return;
  30.         }
  31.  
  32.  
  33.         if (rank == 1)
  34.         {
  35.             this.rank = "Ace";
  36.         }
  37.         else if (rank == 11)
  38.         {
  39.             this.rank = "Jack";
  40.         }
  41.         else if (rank == 12)
  42.         {
  43.             this.rank = "Queen";
  44.         }
  45.         else if (rank == 13)
  46.         {
  47.             this.rank = "King";
  48.         }
  49.         else
  50.         {
  51.             this.rank = "" + rank;
  52.           }
  53.     }
  54.     public String getSuit() 
  55.     {
  56.         return suit;
  57.     }
  58.     public String getRank() 
  59.     {
  60.         return rank;
  61.     }
  62.  
  63. }
  64.  
Aug 13 '10 #1
2 1834
by the way I'm using bluej
Aug 13 '10 #2
TheServant
1,168 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. java.util.Random
  2.  
  3. Randon rand = new Random();
  4.  
  5. myArray[rand.nextInt(myArray.length)]
  6.  
http://download.oracle.com/javase/1....il/Random.html
Aug 16 '10 #3

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

Similar topics

2
by: Calvin79 | last post by:
Hi All, I've just started to try python so forgive the ignorance! I've been using this, or something like it; First = random.choice ('abc') answer = first print answer - no problems there.
1
by: TCORDON | last post by:
How can i tell VB.NET to pick a random number from a predefined series like: 10,10,10,38,38,17,17,17,17,92 TIA
6
by: Pao | last post by:
My code works in this way: I declared a static array in a class (public static int GVetRandom = new int;) that in a for cycle I fill with random numbers. The array gets cleared (clear method) and...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
5
Lazandra
by: Lazandra | last post by:
Hi, I have done a random script which works perfectly fine. The problem that i'm having with is the picture. Below is the script include("connect.php"); mySQL_Select_DB("db220854136");...
9
by: JuAn2226 | last post by:
HI I'M NEW IN VB6. i HAVE PROJECT WHICH REQUIRES ME TO STORE RANDOM NUM IN ARRAY N CAPTURE TIME FOR EACH OF THE NEW NUM IN THE ARRAY FORM. Actually my project is to calculate the average car speed....
10
by: cherryst152 | last post by:
I'm trying to make an array from a query and then randomly select 10 values from the array, but the error i keep getting is that it's not reading as an array. Thanks in advance for anyone who can...
3
by: rhen18 | last post by:
Good day! Im working on a math drill program where the user is asked to answer random math problems that involves the +, -, /, *, and % operators. Now i dont know how to generate these randomly....
1
by: blazinhieu | last post by:
I want to double the score if the cards have the same suit or value and if two cards have the same both suit and value, the score get tripled. And the 2 of clubs is a 20 points. i already wrote the...
7
by: Suprim | last post by:
I am using a timer to call a random number range from 1 to 50 and display it in a label. How can i display the number without repeating the number that have been call out? For example, if number...
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
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.