package com.itheima.day5homework;
import java.util.Random;
import java.util.Scanner;
public class Test6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random r = new Random();
Scanner sc = new Scanner(System.in);
System.out.println("该班级有多少组:");
int team = sc.nextInt();
System.out.println("每组学生的数量:");
int number = sc.nextInt();
int[][] arr = new int[team][number];
int[][] newArr = random(arr);
print(newArr);
groupBujige(newArr);
int bujige = bujige(newArr);
System.out.println(bujige);
groupAverage(newArr);
int average = average(newArr,team,number);
System.out.println(average);
int max = maxAverage(newArr);
System.out.println(max);
}
public static int[][] random(int[][] arr) {
Random r = new Random();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = r.nextInt(101);
}
}
return arr;
}
public static void print(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.println(arr[i][j]);
}
}
}
public static void groupBujige(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
int count1 = 0;
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] < 60) {
count1++;
}
}
System.out.println("第" + (i + 1) + "组不及格人数为:" + count1);
}
}
public static int bujige(int[][] arr) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] < 60) {
count++;
}
}
}
return count;
}
public static void groupAverage(int[][] arr) {
int average = 0;
for (int i = 0; i < arr.length; i++) {
int sum = 0;
for (int j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
average = sum / arr[i].length;
}
System.out.println("第" + (i + 1) + "平均分:" + average);
}
}
public static int average(int[][] arr,int number,int team) {
int average;
int sum = 0;
int i;
int j;
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
}
}
average = sum / number/ team;
return average;
}
public static int maxAverage(int[][] arr) {
int average = 0;
int max =0;
for (int i = 0; i < arr.length; i++) {
int sum = 0;
for (int j = 0; j < arr[i].length; j++) {
sum += arr[i][j];
average = sum / arr[i].length;
}
if(max<average) {
max= average;
}
}
return max;
}
}
上面是一个方法与二维数组的复杂结合,对于一个小白的我,愣是靠自己一个人思考编出来,虽然时间比较长,但是我觉得开始一定要理清思路,不要一味不会就问,要多思考多敲代码,才能慢慢弄懂.他人一开始教导是需要,但是需要一段自己思考阶段,这个阶段或许有点长,但是坚持就能突破自己的能力. |
|