,我有2個陣列名稱和評分和二者的長度是2不顯示正確的比分的java在我的代碼
import java.util.Scanner;
public class Test {
private static Scanner key = new Scanner (System.in);
public static void main (String [] args){
String [] name = new String [2];
int [] score = new int [2];
readInfo(name, score);
showInfo(name, score);
}
public static void readInfo (String [] name, int [] score){
for (int i = 0; i < name.length; i++){
System.out.print("Name no."+(i + 1)+": ");
name [i] = key.next();
for (int j = 0; j < score.length; j++){
System.out.print("Score no."+(j + 1)+": ");
score [j] = key.nextInt();
}
System.out.println("");
}
}
public static void showInfo (String [] name, int [] score){
System.out.println("Name\tScores\n");
for (int i = 0; i < name.length; i++){
System.out.print(name [i]);
for (int j = 0; j < score.length; j++)
System.out.println("\t"+score [j]+"\t");
System.out.println("");
}
}
}
當我輸入姓名和分數,在輸出僅顯示兩個名字的第二分數。
Name no.1: john
Score no.1: 17
Score no.2: 19
Name no.2: jack
Score no.1: 12
Score no.2: 18
Name Scores
john 12
18
jack 12
18
這是我的問題我該如何解決它?
'int [] score = new int [2];'爲2'int's創建空間,但是您有4個分數。 –