我的所有其他方法都是我的类程序正常工作所需的,但是我想不出如何修复我的行输入和求和方法以及我的列输入和求和方法。我也为可笑的长方法名称道歉,这是为作业预先命名的方法。
import java.util.Scanner;
public class Two_D_Array_CMPS280_2016
{// begin class Two_D_Array_CMPS280_2016
public static void main(String args[])
{// begin main method
int[][] brianArray =
{// initialize array and hard code values
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};// end array initialization
// Print segment to print values from methods for brianArray
System.out.print("The sum of all elements is " + Total_2DArray_Elements(brianArray));
System.out.print("\nThe average of all elements is " + Average_2DArray_Elements(brianArray));
System.out.print("The sum is: " + Total_2DArray_Elements_by_Certain_Row_Number(brianArray));
System.out.print("The sum is: " + Total_2DArray_Elements_by_Certain_Column_Number(brianArray));
System.out.print("The largest value is: " + The_Largest_Value_In_Certain_Row_Number(brianArray));
System.out.print("The largest value is: " + The_Largest_Value_In_Certain_Column_Number(brianArray));
}// end main method
public static void printArray(int[][] myArray)
{// begin to print arrrays
for (int i = 0; i < myArray.length; i++)
{
for (int j = 0; j < myArray[i].length; j++)
{
System.out.print(myArray[i][j] + " ");
}
System.out.println();
}
}// end printArray method
public static int Total_2DArray_Elements(int[][] myArray)
{// begin method to sum array values
int total = 0;
for (int i = 0; i < myArray.length; i++)
{
for (int j = 0; j < myArray[i].length; j++)
{
total += myArray[i][j];
}
}
return total;
}
public static int Average_2DArray_Elements(int[][] myArray)
{// begin method to sum array values
int total = 0;
int average = 0;
for (int i = 0; i < myArray.length; i++)
{
for (int j = 0; j < myArray[i].length; j++)
{
total += myArray[i][j];
average = total / 16;
}
}
return average;
}// end method to sum array values
public static int Total_2DArray_Elements_by_Certain_Row_Number(int[][] myArray)
{// begin method to sum array by user input row
java.util.Scanner input = new Scanner(System.in);
int total = 0;
System.out.println("\nPlease enter the row you would like to sum: ");
int row = input.nextInt() - 1;
for (int i = row; i < myArray.length; i++)
{
for (int j = 0; j < myArray[0].length; j++)
{
total += myArray[row][j];
}
}
return total;
}// end method to sum array by user input row
public static int Total_2DArray_Elements_by_Certain_Column_Number(int[][] myArray)
{// begin method to sum array by user input column
java.util.Scanner input = new Scanner(System.in);
int total = 0;
System.out.println("\nPlease enter the column you would like to sum: ");
int column = input.nextInt() - 1;
for (int i = 0; i < myArray.length; i++)
{
for (int j = column; j < myArray[0].length; j++)
{
total += myArray[i][column];
}
}
return total;
}// end method to sum array by user input column
public static int The_Largest_Value_In_Certain_Row_Number(int[][] myArray)
{// begin method to return largest number by user input row
java.util.Scanner input = new Scanner(System.in);
int highestValue = 0;
System.out.println("\nPlease enter the row you would like the highest value from: ");
int row = input.nextInt() - 1;
for (int i = row; i < myArray.length; i++)
{
for (int j = 0; j < myArray[i].length; j++)
{
if (myArray[row][j] > highestValue)
highestValue = myArray[row][j];
}
}
return highestValue;
}// end method to return largest number by user input row
public static int The_Largest_Value_In_Certain_Column_Number(int[][] myArray)
{// begin method to return largest number by user input column
java.util.Scanner input = new Scanner(System.in);
int highestValue = 0;
System.out.println("\nPlease enter the column you would like the highest value from: ");
int column = input.nextInt() - 1;
for (int i = 0; i < myArray.length; i++)
{
for (int j = column; j < myArray[i].length; j++)
{
if (myArray[i][column] > highestValue)
highestValue = myArray[i][column];
}
}
return highestValue;
}// end method to return largest number by user input column
}// end class
答案 0 :(得分:1)
这是 Java 8 魔法,这将有助于你对任何行或列的求和,平均值等
int[][] intArray =
{// initialize array and hard code values
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}
};
//Sum specific column
int columnNo=0;
Integer sumOfColum=Arrays.stream(intArray).mapToInt(i->i[columnNo]).sum();
Double avgOfColum=Arrays.stream(intArray).mapToInt(i->i[columnNo]).average().getAsDouble();
System.out.println(sumOfColum);
System.out.println(avgOfColum);
//Sum of specific row
int rowNo=0;
Integer sumOfRow=Arrays.stream(intArray[rowNo]).sum();
Double avgOfRow=Arrays.stream(intArray[rowNo]).average().getAsDouble();
System.out.println(sumOfColum);
System.out.println(avgOfRow);
答案 1 :(得分:0)
将您的方法更改为以下内容,应该做得好:
public static int Total_2DArray_Elements_by_Certain_Row_Number(int[][] myArray)
{// begin method to sum array by user input row
java.util.Scanner input = new Scanner(System.in);
int total = 0;
System.out.println("\nPlease enter the row you would like to sum: ");
int row = input.nextInt() - 1;
for (int j = 0; j < myArray[0].length; j++) {
total += myArray[row][j];
}
return total;
}// end..
和
public static int Total_2DArray_Elements_by_Certain_Column_Number(int[][] myArray)
{// begin method to sum array by user input column
java.util.Scanner input = new Scanner(System.in);
int total = 0;
System.out.println("\nPlease enter the column you would like to sum: ");
int column = input.nextInt() - 1;
for (int i = 0; i < myArray.length; i++) {
total += myArray[i][column];
}
return total;
}// end m..