I am having trouble getting a player input array that first sorts it from least to most then transfers from one class to a class that checks it against a random array. Here's what I have so far. The checker class is only returning 0's and int counter almost always=4. What am I doing wrong? Thank you in advance for the help!
public static void main(String[] args)
{
int[] player=new int[5];
int[] computer=new int[5];
//creates random numbers to array and asks player if they want to play again
player_input(player);
System.out.println(java.util.Arrays.toString(player));
checker(player,computer,cash,count1,count2,count3,count4,count5);
// calling the functions
}
Is it something wrong in my main method?
public static int[] player_input(int player[]) {
String value;
int ivalue;
int count=0;
int temp;
for(int i=0;i<=4;i++) {
count++;
try{
value = JOptionPane.showInputDialog(null,
"Enter Number "+count+": ","Input Data",JOptionPane.QUESTION_MESSAGE);
ivalue = Integer.parseInt(value);
player[i] = ivalue;
}catch (NumberFormatException ex) {
//handles non int's
}
}
//sorting the array so it can match
if (ivalue > -1 && ivalue<41) {
player[i] = Integer.parseInt(value);
for(int j=i+1;j<=4;j++) {
if(player[i]>player[j]) {
temp=player[i];
player[i]=player[j];
player[j]=temp;
}
return player;
}
Then player input goes to the checker which is only giving out 0's and counter doesn't input the correct amount.
public static int checker(int[] player, int computer[],int cash,int count1,int count2,int count3,int count4, int count5) {
int counter=0;
for (int i=0;i<=4;++i) {
if(player[i]==computer[i]) {
System.out.println(player[i]);
counter++;
}
}
if(counter==0) {
JOptionPane.showMessageDialog(null,"You won nothing, peasent, your total is: $"+cash,"BAD HUMAN",JOptionPane.INFORMATION_MESSAGE);
}
else if(counter==1) {
count1++;
cash=cash+100;
JOptionPane.showMessageDialog(null,"Congratulations! You got: "+counter+" right for $100! Your total is: $"+cash,"GOOD HUMAN",JOptionPane.INFORMATION_MESSAGE);
//... goes to counter==5
return cash;
}