Я хотел бы узнать, как использовать алгоритм сортировки (последний метод кода) в методе «Вопросы», чтобы сортировать имена каждого игрока в заказе. Я не могу поместить его в метод Main, так как для него требуется строка (имена) из цикла, который находится в методе «Вопросы».Алгоритм сортировки [Внедрение в метод]
public static void main(String[] args){
int players = Integer.parseInt(JOptionPane.showInputDialog("Please tell me how many players are taking part in this quiz!"));
/* --------------------------- Questions in Arrays --------------------------- */
String[] question = {"Question 1: ",
"Question 2: ",
"Question 3: ",
"Question 4: ",
"Question 5: " };
String[] answer = {"ans1",
"ans2",
"ans3",
"ans4",
"ans5" };
questions(question, answer, players);
/* --------------------------- Questions in Arrays --------------------------- */
} /* End of Main Method */
/* Questions Method */
public static void questions(String[] question, String[] answer, int n) {
String[] name = new String[n]; // Player Names
int[] playerscore = new int[n]; // Argument for Score
String[] que = new String[question.length]; //Questions for Loops
/* --------------------------- For loop for number of players --------------------------- */
for (int i = 0; i < n; i++)
{
playerscore[i] = 0;
name[i] = JOptionPane.showInputDialog("What is your name player"+ (i+1) +"?");
JOptionPane.showMessageDialog(null,"Hello :"+ name[i] + " Player number " +(i+1)+ ". I hope your ready to start!");
/* --------------------------- Loop in Loop for questions --------------------------- */
for (int x=0; x<question.length; x++) {
que[x] = JOptionPane.showInputDialog(question[x]);
if(que[x].equals(answer[x]))
{
playerscore[i] = playerscore[i] + 1;
}
else {
JOptionPane.showMessageDialog(null,"Wrong!");
}
} // End for loop for Question
System.out.println("\nPlayer: "+(i+1)+ " Name: "+name[i]+"\tScore"+playerscore[i]+" out of 5.);
/* --------------------------- Loop in Loop for questions --------------------------- */
} // End for loop for player number
} //End method questions
государственной статической силы sortNames (имя String []) {
int i;
boolean flag = true; // will determine when the sort is finished
String temp;
while(flag) { // while flag is true
flag = false;
for (i = 0; i < name.length-1; i++) {
if (name[i].compareToIgnoreCase(name[i+1]) > 0) {
temp = name[i];
name[i] = name[i+1]; // swapping
name[i+1] = temp;
flag = true;
}
}
}
}
/* --------------------------- Score Display --------------------------- */
}
Когда я пытаюсь вернуть массив имен из метода вопрос он говорит, что стоимость уже была объявлена. – DanDan111