Reputation: 1
So what does this method need to return to give me the desired results? An array or a 2D array?
I've tried changing String temp into an array like String[][]
but I'm hit with more errors.
// The method
public static String[][] Shuffle (String[][] states){
for ( int i = 0; i < states.length; i++){
for (int j = 0; j <states[i].length; j++){
int i1 = (int)(Math.random() * states.length);
int j1 = (int) (Math.random() * states[i].length);
String temp = states[i][j];
states[i][j] = states[i1][j1];
states[i1][j1] = temp;
return temp;
}
}
}
My code currently works without randomizing the first row in the array but I'd like to randomize the states. The new method would replace this line states[index][1]
.
Upvotes: 0
Views: 54
Reputation: 425033
I think you're code is too complicated, and your method isn't named well.
Try this:
/**
* @return a random [state, capital]
*/
static String[] getRandomState(String[][] states){
return states[(int)(Math.random() * states.length)];
}
and this:
String[] state = getRandomState(states);
System.out.println("What is the capital of " + state[0] + "?");
// get user input as user_ans
gamecounter++;
if (user_ans.equalsIgnoreCase(state[1])) {
System.out.println("Correct!");
correct_ans++;
} else {
System.out.println("Incorrect! The correct answer is " + states[1]);
}
System.out.println();
Upvotes: 1