Help camparing array elements
Okay this is my first question on here. but essentially I have 2 codes (one written by someone else) and mine is supposed to implement their code. and in this code i am supposed to deal out 2 hands of 5 cards. compare each card of 1 hand to the other cards of the same hand.<-- thats where my problem is.
In my code i am only comparing the 1st element of the array to the rest. i need to compare each element to the remainder of the array
(MY CODE)
public class cardTester
{
public static void main(String[] args)
{
Card[] hand1 = new Card[5];
Card[] hand2 = new Card[5];
int pair1;
pair1 =0;
System.out.println("Player1 hand");
for(int x=0; x<1; x++)//<--if i use 5 or hand1.length here it will give me 5 different hands.
{
Card card1 = new Card();
card1.printCard();
System.out.println();
hand1[x] = card1;
for(int y=1; y<5; y++)
{
Card card2 = new Card();
card2.printCard();
System.out.println();
hand1[y] = card2;
if(hand1[x].face() == hand1[y].face())
pair1++;
}
System.out.println(pair1);
}
System.out.println("Player2 hand");
for(int i=0; i<hand2.length; i++)
{
Card card1 = new Card();
card1.printCard();
System.out.println();
hand2[i] = card1;
}
}
}
-----------------------------------------------------------------------------------------------
I am going to see my TA tomorrow reguardless, but i was wondering if anyone here could help me.
Re: Help camparing array elements
Quote:
Originally Posted by
Richmond
Okay this is my first question on here. but essentially I have 2 codes (one written by someone else) and mine is supposed to implement their code. and in this code i am supposed to deal out 2 hands of 5 cards. compare each card of 1 hand to the other cards of the same hand.<-- thats where my problem is.
In my code i am only comparing the 1st element of the array to the rest. i need to compare each element to the remainder of the array
(MY CODE)
public class cardTester
{
public static void main(String[] args)
{
Card[] hand1 = new Card[5];
Card[] hand2 = new Card[5];
int pair1;
pair1 =0;
System.out.println("Player1 hand");
for(int x=0; x<1; x++)//<--if i use 5 or hand1.length here it will give me 5 different hands.
{
Card card1 = new Card();
card1.printCard();
System.out.println();
hand1[x] = card1;
for(int y=1; y<5; y++)
{
Card card2 = new Card();
card2.printCard();
System.out.println();
hand1[y] = card2;
if(hand1[x].face() == hand1[y].face())
pair1++;
}
System.out.println(pair1);
}
System.out.println("Player2 hand");
for(int i=0; i<hand2.length; i++)
{
Card card1 = new Card();
card1.printCard();
System.out.println();
hand2[i] = card1;
}
}
}
-----------------------------------------------------------------------------------------------
I am going to see my TA tomorrow reguardless, but i was wondering if anyone here could help me.
To make your code easy to read and find help, please enclose it with [highlight = java] code here[/java]
Re: Help camparing array elements
i know what you mean, but as you can see i am new to posting on this website and i am unsure on how to make my code look neat the the website.. it looks fine on Dr. Java, but i dont know how to make it look like that here. I can add comment lines to tell which } ends what
Code java:
public class cardTester
{
public static void main(String[] args)
{
Card[] hand1 = new Card[5];
Card[] hand2 = new Card[5];
int pair1;
pair1 =0;
System.out.println("Player1 hand");
for(int x=0; x<5; x++)
{
Card card1 = new Card();
card1.printCard();
System.out.println();
hand1[x] = card1;
for(int y=1; y<5; y++)
{
Card card2 = new Card();
card2.printCard();
System.out.println();
hand1[y] = card2;
if(hand1[x].face() == hand1[y].face())
pair1++;
}//end for loop 2
System.out.println(pair1);
}//end for loop 1 aka nested
System.out.println("Player2 hand");
for(int i=0; i<hand2.length; i++)
{
Card card1 = new Card();
card1.printCard();
System.out.println();
hand2[i] = card1;
}//end for loop player2 hand
}//end main
}// end class
Re: Help camparing array elements
The highlighting was all that was necessary, thanks!
Here are a list, in order, of things that I noted about your code.
1: You naming for your class should start with a capitol letter. (EG String, ArrayList, Card, System)
2: You spacing is incorrect, this should help with most of those problems (Also your variable names)
3. Your logic here is... a bit flawed. How wouold you do this on a piece of paper? (I'd recommend grid paper because you are using arrays). It looks like you are using this as a 2d array, but I can't tell.
Re: Help camparing array elements
1)This is just a Tester that i will later convert to my EMACS java code. that has a capital letter.
2) okay.
3) on a piece of paper i read 5 random cards compare card 1 to card 2 card 1 to 3.. card 1 to 5.. card 2 to 3-5 card 3-5 card 4-5. its NOT a 2d array. its 1d because i only care about the number valued with that card.
and if you think its flawed because of the for(int x=0; x<1; x++)
in my first post i had it as
for(int x=0; x<1; x++)//<--if i use 5 or hand1.length here it will give me 5 different hands.
telling you why i had it at 1