2015-11-20 34 views
0

這是我接受學生姓名和分數的二維字符串數組的方法。從2D字符串數組中提取整數部分以獲得學生成績的最低分數。數組包含學生姓名和分數

public int getMinScore(String[][] a) { 

    String intVal; 
    String s = a[0][0]; 
    intVal = s.replaceAll("[^0-9]", ""); 
    int lowest = Integer.parseInt(intVal); 
    int num = 0; 

    for(int i = 1; i < a.length; i++) { 

     for(int j = 0; j < a[i].length; j++) { 

      s += a[i][j]; 
      intVal = s.replaceAll("[^0-9]", ""); 
      num = Integer.parseInt(intVal); 

      if(num < lowest){ 
       lowest = num; 

      } 
     } 
    } 
    return lowest; 
} 

這是我一直得到的結果:

Enter student name 
will 
Enter test score 
100 
Enter student name 
bill 
Enter test score 
80 
will 100 
bill 80 
MIN SCORE IS: 100 
+1

什麼是你的問題?你如何從方法期望? – Abdelhak

+0

看起來像「s + =」應該只是「s =」。也不確定你的數組邏輯是正確的,因爲你不檢查[0] [1] [0] [2]等。 – mmulholl

+0

@Abdelhak基本上我要求用戶輸入多個學生的名字和測試分數並存儲該信息在一個2D字符串數組中。我希望能夠隔離字符串的整數部分(測試分數),以便我可以遍歷數組來查找輸入學生的最低分數。 – Jay

回答

0

要找到minum嘗試使用此

int smallest_number(int b[][]) 
{ 
    int min = b[0][0]; 
    int x,y; 

for (x = 0; x < b.length; x++) 
{ 
    for (y = 0; y < b[x].length; y++) 
    { 
     if (min > b[x][y]) 
     { 
      min = b[x][y]; 
     } 
    } 
} 

return min; 
}