私は、ユーザーにゲームプレイヤーの数、ゲームプレイヤーの名前、スコアを入力し、スコアの降順でスコアを印刷するように促すプログラムを作成しようとしています。ループを使用して名前を配列に保存するにはどうすればよいですか?
名前とスコアの配列を使用する必要があります。残念ながら、これは私が持っているすべてです。
サンプル出力
Enter the number of players: n
Enter the name of the player: Ash
Enter the player's score: 1200
Enter the name of the player: Brock
Enter the player's score: 900
Enter the name of the player: Misty
Enter the player's score: 1300
Misty 1300.0
Ash 1200.0
Brock 900.0
import java. util.*;
public class HomeworkAssignment12 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of game players: ");
int numOfPlayers = input.nextInt();
String[] names = new String[numOfPlayers];
double[] scores = new double[numOfPlayers];
//Trying to store the names the user inputs into the names[] array
for (int i = 0; i < names.length; i++) {
int index = i;
System.out.println("Enter a game players name: ");
names[index] = input.nextLine();
System.out.println("Enter the player's score: ");
scores[index] = input.nextDouble();
//used to check what the loop is doing each iteration
System.out.println(i);
}//end for
}//end main
}//end class
「残念ながら、これは私が持っているすべてです」だから、これはどうしますか?それはどうしますか?何をやったの? –
もっとお試しください。あなたが正しい道にいるように見えます。そのforループ内では、プレイヤーのスコアを入力して配列に格納するように依頼する必要があります。難しい部分は、配列をソートし、配列のインデックスをまとめて保持することです。がんばろう! – Sedrick
ヒント: 'input.nextDouble()' –