Random suit and card with arrays
Hello!
I am having some practice with arrays, and I am trying to make a program that picks a random suit and then picks a random card from that suit. Here is the code, I am not finished with it yet:
Code java:
public class RandomSuitandCard {
public static void main(String[] arg) {
int[] Diamond;
int[] Heart;
int[] Club;
int[] Spade;
int i; //position in array
int i2 = 0; //number to be out in array
Diamond = new int[12];
Heart = new int[12];
Club = new int[12];
Spade = new int[12];
for (i=0, i=12, i++) {
i2++;
Diamond[i]=i2; }
for (i=0, i=12, i++) {
i2++;
Heart[i]=i2; }
for (i=0, i=12, i++) {
i2++;
Club[i]=i2;
}
for (i=0, i=12, i++) {
i2++;
Spade[i]=i2;
}
}
}
The for statements are as follows:
Code java:
for (i=0, i=12, i++) {
i2++;
Diamond[i]=i2; }
But the i++ is underlined in red and it says:
Syntax error, insert "; ; ) Statement" to complete ForStatement
This doesn't make any sense! Please help!
Re: Random suit and card with arrays
Quote:
for (i=0, i=12, i++) {
for statements don't use commas like that. If you are unsure check out The for Statement in Oracle's Tutorial.
-----
It would be a very good idea to follow Java coding conventions and begin variables with a lowercase letter: diamond etc.
Re: Random suit and card with arrays
Oh thank you so much!!! It solved it! Thanks again.
Re: Random suit and card with arrays
Ok, new problem.
I made an array array to hold all the suits, so here is the updated code:
Code java:
import java.util.Random; //imports the class needed to pick a random slot from the arrays, I did not want to you use Math.random and make a switch here, that is an alternative
public class RandomSuitandCard {
public static void main(String[] arg) {
int[][] suits;
int[] diamond;
int[] heart;
int[] club;
int[] spade;
int i; //position in array
int i2 = 0; //number to be out in array
int randomSuit; //random suit chosen
int randomCard; //random card from random suit chosen
String output;
Random random = new Random();
suits = new int[3][44];
diamond = new int[12];
heart = new int[12];
club = new int[12];
spade = new int[12];
suits[0] = diamond;
suits[1] = heart;
suits[3] = club;
suits[4] = spade;
for (i=0; i<=12; i++) {
i2++;
diamond[i]=i2; }
for (i=0; i<=12; i++) {
i2++;
heart[i]=i2; }
for (i=0; i<=12; i++) {
i2++;
club[i]=i2;
}
for (i=0; i<=12; i++) {
i2++;
spade[i]=i2;
}
randomSuit = (random.nextInt(3));
switch (randomSuit) {
case 0:
suits = suits[0];
}
}
}
So, at the end, at
Code java:
randomSuit = (random.nextInt(3));
switch (randomSuit) {
case 0:
suits = suits[0];
I am trying to get the random suit. The suits[0] is underlined in red and it says:
Type mismatch: cannot convert from int[] to int[][]
But if you look at the top, it obviously says that it is an array array:
Help?
Re: Random suit and card with arrays
How would you assign a single array to double array?
Read the error message carefully and you'll hopefully get to know.
Re: Random suit and card with arrays
Ok, here's what I think.
If your telling me that I need to display it as a suits[][] (array array) than I have already tried that and it does nothing. If you mean that I am trying to assign an array into an array array, then shouldn't that be what it does? I mean, an array is a box that contains numbers. So, an array array is a box of boxes of numbers, or an array that holds arrays, right?
If you saying that I need to make the one of the suits arrays that are supposed to contain the cards into something different, than please explain.
Re: Random suit and card with arrays
What i mean to say is, let's assume you have a two dimensional array,
Code :
int [][] array = new int[5][];
What does this mean?
And when you do,
what do you mean by this?
Tell us, what do you want to do and why do you need to do this?