0
import java.util.Random;
public class Accidents{
static final int DAYS = 31;
static final int HOURS = 24;
private int[][] accidents = new int[DAYS][HOURS];
public Accidents(){
Random rand = new Random();
accidents[DAYS][HOURS] = rand.nextInt(10);
}
}
這顯然不起作用。我曾嘗試使用tester.I想知道我應該如何編寫構造函數,以便它生成從0到9的隨機整數?謝謝。如何在構造函數中使用隨機整數填充數組? (2D陣列)
您可以忽略下面的內容。
public int totalAccidents(){
int total = 0;
for(int i=0; i<accidents.length; i++){
for(int j=0; j<accidents[0].length; j++){
total += accidents[i][j];
}
}
return total;
}
public int mostAccidents(){
int sum = 0;
int max = 0;
for (int i=0;i<24;i++){
for(int j=0; j<accidents.length; j++){
sum += accidents[j][i];
}
if (sum> max)
max = sum;
}
return max;
}
public void printArray(){
for(int j=0; j<accidents.length; j++){
for(int k=0; k<accidents[0].length; k++){
System.out.print(accidents[j][k]+" ");
}
System.out.println();
}
}
}