编译此代码时只显示最后一个输入的分数
import java.util.*;
public class TestII {
static Scanner key = new Scanner (System.in);
static int countNa = 0;
static int countSc = 0;
static int numSc;
static int numNa;
static int [] scores = null;
static String [] names = null;
public static void main(String[] args) {
System.out.println("How many name and scores");
System.out.print("N> ");
numNa = key.nextInt();
System.out.print("S> ");
numSc = key.nextInt();
names = new String [numNa];
scores = new int [numSc];
readNameScores( );
showNamesScores ();
}
public static void readNameScores (){
for (int i = 0; i < names.length; i++){
countNa++;
System.out.print("Name "+countNa+": ");
names [i] = key.next();
for (int j = 0; j < scores.length; j++){
countSc++;
if (countSc > scores.length){
countSc = 0;
countSc++;
}
System.out.print("\tScore "+countSc+": ");
scores [j] = key.nextInt();
}
}
}
public static void showNamesScores (){
System.out.println("");
for (int i = 0; i < names.length; i++){
System.out.print(names [i]+"\t");
for (int j = 0; j < scores.length; j++){
System.out.print(scores [j]+" ");
}
}
}
}
输出样本
有多少名字和分数 N'GT; 2 S&GT; 2 名称1:最大 得1:2 得分2:4 名字2:迈克 得1:3 得分2:2
最多3 2迈克3 2
答案 0 :(得分:0)
您的scores
数组用于保存只有一个名称的分数。您应该将其更改为2D数组。
static int [][] scores = null;
...
names = new String [numNa];
scores = new int [numNa][numSc];
...
public static void readNameScores (){
for (int i = 0; i < names.length; i++){
System.out.print("Name "+(i+1)+": ");
names [i] = key.next();
for (int j = 0; j < scores.length; j++){
System.out.print("\tScore "+(j+1)+": ");
scores[i][j] = key.nextInt();
}
}
}