/* * (生徒の並べ替え) 生徒の数、生徒の名前、および点数を入力するようユーザーに促し、生徒の名前を点数の降順で出力するプログラムを作成します。*/
package homework6_17;
import java.util.Scanner;
public class Homework6_17 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of students: ");
int numberOfStudents = input.nextInt();
String[] names = new String[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter the name of student: ");
names[i] = input.nextLine();
}
double[] scores = new double[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter the score of student: ");
scores[i] = input.nextDouble();
}
String temps = "";
double temp = 0;
double max = scores[0];
for(int i = 0; i<(scores.length-1); i++){
if(scores[i+1]>scores[i]){
temp=scores[i+1];
scores[i]=scores[i+1];
scores[i+1]=scores[i];
temps = names[i+1];
names[i]=names[i+1];
names[i+1]=names[i];
}
}
for(int i = 0 ; i<(scores.length-1); i++)
System.out.println(names[i]+ " " + scores[i]);
}
}
このプログラムを実行すると; 走る:
生徒数を入力してください: 3
生徒の名前を入力してください: 生徒の名前を入力してください: a
学生の名前を入力してください: b
生徒の点数を入力してください: c
Exception in thread "main" java.util.InputMismatchException
// 「生徒の名前を入力してください:」が 1 回ではなく 2 回表示されました。