thomasrive
September 27th, 2010, 03:12 AM
Hello everybody. I know the rules on posting homework, and I have read the FAQ. I'm not asking for somebody to do my program for me, I've already written the entire program but have been struck down by a bug which shouldn't be occurring. This is a simple GoFish program, and my problem resides in a misbehaving if statement.:confused:
public static int[] cardArray = new int[39]; public static int[] cardSelected = new int[8];
public void cardGame() {
Random myRandom = new Random();
//int cardDeal = myRandom.nextInt(40);
int cardGiven =0;
for(int i=0;i<8;i++){
int cardDeal = myRandom.nextInt(39);
cardGiven = myRandom.nextInt(8);
cardArray[i] = cardDeal;
cardSelected[i] = cardGiven;
}
}
@Action
public void fishing() {
clear();
cardGame();
Random cardRandom = new Random();
int alpha = cardRandom.nextInt(39);
//Here Resides the misbehaving if Statement
if(cardArray[alpha] == cardSelected[0] || cardArray[alpha] == cardSelected[1]
|| cardArray[alpha] == cardSelected[2]||cardArray[alpha] == cardSelected[3]||
cardArray[alpha] == cardSelected[4]||cardArray[alpha] == cardSelected[5]||cardArray[alpha] == cardSelected[6]
||cardArray[alpha] == cardSelected[7]||cardArray[alpha] == cardSelected[8]){
result.setText("You Won");
String alpha2 = "";
alpha2 += alpha;
debug.setText(alpha2);
}else{
result.setText("");
result.setText("Go Fish");
}
I have reviewed my code, and it is working only partly. When there is a match, the GUI I set up returns "You Won". However the problem occurs when there is no match made by the if statement, in which case nothing happens when the GUI is supposed to return "Go Fish". I'd be much appreciated if somebody could point me to the error if it exists. Made it using NetBeans.:P
public static int[] cardArray = new int[39]; public static int[] cardSelected = new int[8];
public void cardGame() {
Random myRandom = new Random();
//int cardDeal = myRandom.nextInt(40);
int cardGiven =0;
for(int i=0;i<8;i++){
int cardDeal = myRandom.nextInt(39);
cardGiven = myRandom.nextInt(8);
cardArray[i] = cardDeal;
cardSelected[i] = cardGiven;
}
}
@Action
public void fishing() {
clear();
cardGame();
Random cardRandom = new Random();
int alpha = cardRandom.nextInt(39);
//Here Resides the misbehaving if Statement
if(cardArray[alpha] == cardSelected[0] || cardArray[alpha] == cardSelected[1]
|| cardArray[alpha] == cardSelected[2]||cardArray[alpha] == cardSelected[3]||
cardArray[alpha] == cardSelected[4]||cardArray[alpha] == cardSelected[5]||cardArray[alpha] == cardSelected[6]
||cardArray[alpha] == cardSelected[7]||cardArray[alpha] == cardSelected[8]){
result.setText("You Won");
String alpha2 = "";
alpha2 += alpha;
debug.setText(alpha2);
}else{
result.setText("");
result.setText("Go Fish");
}
I have reviewed my code, and it is working only partly. When there is a match, the GUI I set up returns "You Won". However the problem occurs when there is no match made by the if statement, in which case nothing happens when the GUI is supposed to return "Go Fish". I'd be much appreciated if somebody could point me to the error if it exists. Made it using NetBeans.:P