2017-07-07 58 views
0

,我有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 

這是我的問題我該如何解決它?

+1

'int [] score = new int [2];'爲2'int's創建空間,但是您有4個分數。 –

回答

0

您有四個總分數和一個只有兩個數組的數組。當您運行readInfo時,第二個名稱的分數將覆蓋從第一個名稱存儲在那裏的scores數組中的數據。您需要在數組中有足夠的空間來存儲所有分數,或者選擇不同的方式來存儲這些數據。

0

你能解決這個問題是這樣的:

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 [name.length*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 = i*2; j < name.length+i*2; 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 = i*2; j < name.length +i*2; j++) 
      System.out.println("\t"+score [j]+"\t"); 


     System.out.println(""); 
    } 

} 
} 

這裏發生了什麼就是你的名字長度爲2的數組,你要分數爲每個。這意味着你的分數陣列需要具有名稱的長度* 2:

int [] score = new int [name.length*2]; 

然後,輸入的得分時,總分索引0和1需要對人1,索引2只3需要對人2,因此:

for (int j = i*2; j < name.length+i*2; j++){ 

     System.out.print("Score no."+(j + 1)+": "); 
     score [j] = key.nextInt(); 


    } 

之後,同樣的事情需要做的閱讀

1

對於第二次迭代 - 關於第二個名稱 - 要覆蓋第一組中輸入該分數。您需要將得分數組設置爲維度爲2x2的二維數組。

int[][] score = new int[2][2]; 

然後,在嵌套for循環,參考分數是這樣的:

for(int i = 0; i < names.length; i++) { 
    ... 
    for(int j = 0; j < score[0].length; j++) { 
      score[i][j] // score referenced like this 
    } 
    ... 
} 

這樣,我們具有長度的兩個對於每個名稱的兩個分開的陣列。

+0

如果分數數組長度爲3,該怎麼辦? –

+0

如果您需要一個可變長度的容器來保存您的分數,請考慮一個ArrayList,您可以在其中添加任意數量的分數 –

+0

如果您確定分數數組需要長度爲三,那麼您可以更改陣列。 int [] [] score = new int [i] [j]其中i是名稱的數量,j是分數的數量。如果分數的數量是可變的,那麼您需要查看像Matt Solarz提到的ArrayList –