Compare the index of 2 String arrays with different values Java -
i have 2 parallel arrays: first contains state names, second capitals of states.
i'm making quiz randomly generates state asks user enter capital of state. once input received want call method check if index of capital entered same index of state goes with.
ie: statearray[0] = "new york" , capitalarray[0] = "albany".
check answer method
public static void checkanswer(string[]statearray, string capitalarray, string answer) { int index; (int = 0; < capitalarray.length; i++){ if(capitalarray[i].equalsignorecase(answer)){ index = i; } } if(capitalarray[index] == statearray[index]) { system.out.println("correct"); } else { system.out.println("incorrect"); } } i know second if statement wrong. how can compare 2 arrays using index users answer found in capitalarray?
boolean checkanswer(string[] statearray, string[] capitalarray, string displayedstate, string answer) { (int = 0; < statearray.length; i++) { if (statearray[i].equals(displayedstate) && capitalarray[i].equals(answer)) { return true; } } return false; } or something. key need pass in represent state displayed.
Comments
Post a Comment