2016-04-10 22 views
0

Java'daki 2d dizisinin her sütunundaki maksimum değeri bulmaya çalışıyorum. Her konu/modül için maksimum işareti bulmak isteyen soru ingilizce versiyonu. Her bir sütuna nasıl yalıtılacağını bilmeden maksimum değeri bulmak için bir for-loop uyguladık.2d dizisinin her sütununda maksimum ve minimum değerler bulma Java

public static void main(String[] args) { 

    double max=0; 
    double mark; 
    double id; 
    double [][] studs = new double [2][3]; 

    Scanner fromKeyboard = new Scanner (System.in); 

    for (int studentNo = 0; studentNo < 2; studentNo++) { 
     System.out.println("enter student ID number for student " + (studentNo+1)); 
     id = fromKeyboard.nextDouble(); 
     studs[studentNo][0] = id; 
     for (int moduleNo = 1; moduleNo < 3; moduleNo++) { 
      System.out.println("Enter users mark for module " + moduleNo); 
      mark = fromKeyboard.nextDouble(); 
      studs[studentNo][moduleNo] = mark; 
     } 
    } 

    for (int col=1;col<3;col++){ 
     for (int row=0;row<2;row++){ 
      if(studs [row][col]>max) { 
       max=studs[row][col]; 
      } 
     } 

     System.out.println(max); 
    } 
    } 

cevap

0

Her yeni sütunun başlangıcında max'u sıfırlamanız gerekir.

+0

Teşekkürler, bu, sıfırdan öğrenirken özlediğin şeyler. – theghid

0

Daha iyi bir yolun farklı bir şekilde düşünmek olduğunu düşünüyorum: Neden Konuları satırlara yerleştirmiyor ve Öğrencileri sütunlara yerleştirmiyorsunuz? Sonra sadece her satırı tara.

İlgili konular