我在将 2d 数组传递给我的方法,然后打印它们时遇到问题,并且在某些数据字段中也找到了最低值



所以我有很好的所有信息,但我有 3 个问题,打印方法,正确传递数组,在我的 getMinValue 方法中,我试图从数组中获取最小的数字 [0][2]、[1][2]、[2][2]、[3][2],[4][2]。我拉错了最低数字。任何帮助将不胜感激!(忽略我的评论)

public class MorenoJonathonSnowFallTotal
{
public static void main(String[] args)
{
double [][] array={{3.6,24.6,31.8,26.4,17.5},
{4.7,29.1,33.0,29.2,17.7},{5.5,23.3,41.0,26.7,14.4},
{4.4,18.8,36.1,24.4,11.1},{2.1,10.1,18.8,18.8,8.9}};

/**for(int i=0;i<5;i++){
for(int j=0;j<5;j++) {
System.out.print(array[i][j]+" ");
}
System.out.println("");
} */
//average by city

//average by month
System.out.println(monthAv);
}
public static double getMinValue(double[][] numbers) {
double minValue = numbers[0][2];
for (int j = 1; j < 3; j++) {
for (int i = 2; i < 3; i++) {
if (numbers[j][i] < minValue ) {
minValue = numbers[j][i];
}
}
}
return minValue ;
}
public void monthAv(double [][] arr)
{
double [][] array=arr;
System.out.println("");
System.out.println("The average of November is "+(arr[0][0]+arr[1] . 
[0]+arr[2][0]+arr[3][0]+arr[4][0])/5);
System.out.println("The average of December is "+(arr[0][1]+arr[1] . 
[1]+arr[2][1]+arr[3][1]+arr[4][1])/5);
System.out.println("The average of January is "+(arr[0][2]+arr[1] . 
[2]+arr[2][2]+arr[3][2]+arr[4][2])/5);
System.out.println("The average of Febuary is "+(arr[0][3]+arr[1] . 
[3]+arr[2][3]+arr[3][3]+arr[4][3])/5);
System.out.println("The average of March is "+(arr[0][4]+arr[1] . 
[4]+arr[2][4]+arr[3][4]+arr[4][4])/5);
}
public void cityAv(double[][] arr)
{
double [][] array=arr;
System.out.println("The average snow fall of Detroit is "+(array[0] . 
[0]+array[0][1]+array[0][2]+array[0][3]+array[0][4])/5);
System.out.println("The average snow fall of Chicago is "+(array[1] . 
[0]+array[1][1]+array[1][2]+array[1][3]+array[1][4])/5);
System.out.println("The average snow fall of Boston is "+(array[2] . 
[0]+array[2][1]+array[2][2]+array[2][3]+array[2][4])/5);
System.out.println("The average snow fall of New York is "+(array[3] . 
[0]+array[3][1]+array[3][2]+array[3][3]+array[3][4])/5);
System.out.println("The average snow fall of Washington D.C is "+ . 
(array[4][0]+array[4][1]+array[4][2]+array[4][3]+array[4][4])/5);
}
}

这里有太多的语法错误,我什至不打算提及它们。我建议您在互联网上查找一些Java教程或书籍,因为它们很多。此外,代码不完整。另外,这个问题不是很清楚。

假设数组的每一行都是城市的值,这些行的每一列都是给定月份城市的值:

阅读注释以大致了解此问题,同时遵循以下代码

package morenojonathonsnowfalltotal;
public class MorenoJonathonSnowFallTotal {

// Register cities
public static final String[] CITIES = {
"Detroid",
"Chicago",
"Boston",
"New York",
"Washington D.C."
};
// Register months
public static final String[] MONTHS = {
"November",
"December",
"January",
"February",
"March"
};
// ROW = City!
// COL = Month!
public static final double[][] DATA_2DARRAY = {
//1º row will be temperatures of Detroid on November, December...
//2º row will be temperatures of Chicago on November, December... AND SO ON AND SO FORTH
{3.6, 24.6, 31.8, 26.4, 17.5},
{4.7, 29.1, 33.0, 29.2, 17.7},
{5.5, 23.3, 41.0, 26.7, 14.4},
{4.4, 18.8, 36.1, 24.4, 11.1},
{2.1, 10.1, 18.8, 18.8, 8.9}
};
public static void main(String[] args) {
int column = 2;
System.out.println("The lowest value for column "+column+" is: "+GetMinimumColumnValue(column));
// You might mean or want...
System.out.println("The lowest total snowfall on "+MONTHS[column]+" is: "+GetMinimumColumnValue(column));
// Separate outputs.
System.out.println("");System.out.println("");System.out.println("");
ComputeMonthsAverageSnowfall();
// Separate outputs
System.out.println("");System.out.println("");System.out.println("");
ComputeCitiesAverageSnowfall();
}
public static double GetMinimumColumnValue(int column) {
/* 
Init to the maximum double value so we ensure 
that the value will be updated on the first iteration
*/
double lowest = Double.MAX_VALUE;
double current;
// Traverse each month looking for the minimum value.
for (int i = 0; i < MONTHS.length; i++) {
current = DATA_2DARRAY[i][column];
/* Update the minimal value if the current 
element is lesser than the lowest we've found so far*/ 
if(current<lowest) lowest=current;
}
return lowest;
}
public static void ComputeMonthsAverageSnowfall() {
double average;
for (int i = 0; i < MONTHS.length; i++) {
// Compute it's average snowfall
average = ComputeAverage(GetColumn(i));
// Note that this time we have to get it's COLUMN values. hence GetColumn method.
System.out.println("The average snowfall on "+MONTHS[i]+" was "+average);
}
}
/**
* Given an index of a column, copies all its values and returns it as an array.
* @param idx_col
* @return 
*/
public static double[] GetColumn(int idx_col){
// A Month register will hold as many registers as cities there are.
// HENCE
// The column will hold as many rows as the original data
double[] col = new double[CITIES.length];
// For each city, copy its values.
for (int i = 0; i < col.length; i++) {
col[i] = DATA_2DARRAY[i][idx_col];
}
return col;
}
public static void ComputeCitiesAverageSnowfall() {
double average;
// For each city...
for (int i = 0; i < CITIES.length; i++) {
// Compute it's average snowfall
average = ComputeAverage(DATA_2DARRAY[i]);
System.out.println("The average snowfall on "+CITIES[i]+" was "+average);
}
}

/**
* Computes the average value of this double array
* @param array
* @return 
*/
public static double ComputeAverage(double[] array){
double average = 0;
// Sum each value from the array we want to compute average.
for (int i = 0; i < array.length; i++) {
average = average + array[i];
}
// Return that sum divided by the total amount of elements that were in the array
return (average/((double) array.length));
}
}

最新更新