Java 编译此代码时,只显示上次输入的分数
编译此代码时,只显示上次输入的分数Java 编译此代码时,只显示上次输入的分数,java,Java,编译此代码时,只显示上次输入的分数 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;
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]+" ");
}
}
}
}
import java.util.*;
公共类测试{
静态扫描仪键=新扫描仪(System.in);
静态int countNa=0;
静态int countSc=0;
静态int-numSc;
静态整数;
静态int[]分数=空;
静态字符串[]名称=null;
公共静态void main(字符串[]args){
System.out.println(“多少姓名和分数”);
系统输出打印(“N>”);
numNa=key.nextInt();
系统输出打印(“S>”);
numSc=key.nextInt();
名称=新字符串[numNa];
分数=新整数[numSc];
readNameScores();
shownamescores();
}
公共静态void readNameScores(){
for(int i=0;i分数.长度){
countSc=0;
countSc++;
}
系统输出打印(“\tScore”+countSc+”:”;
分数[j]=key.nextInt();
}
}
}
公共静态void shownamescores(){
System.out.println(“”);
for(int i=0;i
输出样本
有多少名字和分数
N> 二,
S> 二,
名称1:max
比分1:2
比分2:4
姓名2:迈克
比分1:3
比分2:2
最多3 2个mike 3 2您的
分数
数组仅用于保存单个姓名的分数。您应该将其更改为二维阵列
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();
}
}
}
static int[][]分数=null;
...
名称=新字符串[numNa];
分数=新整数[numNa][numSc];
...
公共静态void readNameScores(){
for(int i=0;i
如果有两个以上的名字和分数?@PouriaMohseni,只要每个名字的分数相同,就应该使用相同的代码。