所以昨天我求助了,很多人帮我解决了我的问题,我非常感谢那些人。然而,我遇到了我的第二个问题,我从今天早上开始尝试解决这个问题,现在它早上几乎凌晨2点就已经解决了,直到我解决之前我都不会睡觉这个问题。我不会撒谎,所以这是我测试我的基本知识的功课。我知道我在某个地方犯了重大错误,所以请帮我指出,所以我可以解决它们。谢谢
输出应为:
1, 2, 3, ave=2
4, 5, 6, ave=5
aver=2.5, 3.5, 4.5
这是我目前的代码:
import java.util.Scanner;
public class Ex {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a number for rows: ");
int rows = input.nextInt();
System.out.print("Enter a number for columns: ");
int columns = input.nextInt();
int[][] array = new int[rows][columns];
System.out.println("Enter the numbers in array: ");
for(int i=0 ; i<rows ; i++)
{
for(int j=0 ; j<columns ; j++)
{
array[i][j] = input.nextInt();
}
}
for(int i=0 ; i<rows ; i++)
{
for(int j=0 ; j<columns ; j++)
{
System.out.print(array[i][j] + " , ");
}
System.out.println("\n");
}
}
double averageRow(int[][] array) {
int rowTotal = 0;
double average = 0;
for (int rows = 0; rows < array.length; rows++) {
for (int columns = 0; columns < array[rows].length; columns++) {
rowTotal += array[rows][columns];
}
average = rowTotal / array[rows].length;
System.out.println(average);
rowTotal = 0;
}
return rowTotal;
}
double averageColumn(int[][] array) {
int columnTotal = 0;
double average = 0;
for (int columns = 0; columns < array.length; columns++) {
for (int rows = 0; rows < array[columns].length; rows++) {
columnTotal += array[rows][columns];
}
average = columnTotal / array[columns].length;
System.out.println(average);
columnTotal = 0;
}
return columnTotal;
}
}
答案 0 :(得分:0)
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows");
int r = sc.nextInt();
System.out.println("Enter number of columns");
int c = sc.nextInt();
System.out.println("Enter values");
int[][] matrix = new int[r][c];
for (int i = 0; i < r; i++)
{
float rowSum = 0f;
for (int j = 0; j < c; j++)
{
matrix[i][j] = sc.nextInt();
rowSum += matrix[i][j];
}
System.out.println("Average of row " + i + " " + rowSum / c);
}
for (int i = 0; i < c; i++)
{
float columnSum = 0f;
for (int j = 0; j < r; j++)
{
columnSum += matrix[j][i];
}
System.out.println("Average of column " + i + " " + columnSum / r);
}
sc.close();
}
您可以在读取数据时找到每行总和。对于列平均运行循环。希望你能睡觉:)
答案 1 :(得分:0)
您无需调用其他功能即可进行打印。 的 的
的import java.util.Scanner;
public class test8 {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a number for rows: ");
int rows = input.nextInt();
System.out.print("Enter a number for columns: ");
int columns = input.nextInt();
int[][] array = new int[rows][columns];
System.out.println("Enter the numbers in array: ");
for(int i=0 ; i<rows ; i++)
{
for(int j=0 ; j<columns ; j++)
{
array[i][j] = input.nextInt();
}
}
int rowSum = 0;
int colSumArr[] = new int[columns];
for(int i=0 ; i<rows ; i++)
{
for(int j=0 ; j<columns ; j++)
{
rowSum = rowSum + array[i][j];
colSumArr[j] = colSumArr[j] + array[i][j];
System.out.print(array[i][j] + " , ");
}
System.out.println( " ave=" + (double)rowSum/columns);
rowSum = 0;
}
System.out.printf("aver=");
for(int i=0;i<columns;i++){
if(i!=columns -1)
System.out.print((double)colSumArr[i]/rows + ", ");
else
System.out.print((double)colSumArr[i]/rows);
}
}
}
的