Java 练习
#//懂的懂
1、定义一个数组来存储12个学生的成绩{72,89,65,58,87,91,53,82,71,93,76,68},计算并输出学生的平均成绩。
public class Day1 {
public static void main(String[] args) {
int a[] = new int[] { 72, 89, 65, 58, 87, 91, 53, 82, 71, 93, 76, 68 };
int j = 0;
for (int i = 0; i < a.length; i++) {
j += a[i];
}
System.out.println(j / (a.length));
}
}
2、定义一个数组来存储12个学生的成绩{72,89,65,58,87,91,53,82,71,93,76,68},统计各成绩等级(90分以上为‘A’,8089分为‘B’,7079分为‘C’,60~69分为‘D’,60分以下为E)学生人数,并将其放入到数组count中,其中:count[0]存E级的人数,count[1]存D级的人数,……,count[4]存A级的人数。
public class Day2 {
public static void main(String[] args) {
int a[] = new int[] { 72, 89, 65, 58, 87, 91, 53, 82, 71, 93, 76, 68 };
int count[] = new int[] { 0, 0, 0, 0, 0 };
for (int i = 0; i < a.length; i++) {
if (a[i] >= 90)
count[0]++;
else if (a[i] >= 80)
++count[1];
else if (a[i] >= 70)
++count[2];
else if (a[i] >= 60)
++count[3];
else if (a[i] < 60)
++count[4];
}
String b[] = new String[] { "A", "B", "C", "D", "E" };
for (int i = 0; i < count.length; i++) {
System.out.println(b[i] + "等级:" + count[i] + "人");
}
}
}
3、定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数
//数字不一样,个数也不一样
import java.util.*;
public class Day1 {
public static void main(String[] args) {
int a[] = new int[] { 72, 89, 65, 58, 87, 91, 53, 82, 71, 93, 76, 68 };
Arrays.sort(a);
System.out.println("最大值:" + a[a.length - 1]);
System.out.println("最小值:" + a[0]);
int j = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0)
j++;
}
System.out.print("偶数个数:" + j + "\n奇数个数:" + + (a.length - j));
}
}
4、有以下四组数据:
A:22,66,44
B:77,33,88
C:25,45,65
D:11,66,99
要求:使用二维数组Array1来存储以上数据,然后对其求和