import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
/**
* @author Peter Cheung
* @user PerCheung
* @date 2021/7/23 17:37
* <p>
* 1、定义一个数组来存储12个学生的成绩{72,89,65,58,87,91,53,82,71,93,76,68},
* 统计各成绩等级(90分以上为‘A’,80~89分为‘B’,70~79分为‘C’,60~69分为‘D’,60分以下为E)学生人数,
* 并将其放入到数组count中,其中:count[0]存E级的人数,count[1]存D级的人数,……,count[4]存A级的人数。
* <p>
* <p>
* 2、从键盘输入8个整数存放在一个数组中,然后将奇数和偶数分别存入到两个不同的数组中,
* 并按奇数、偶数交替的顺序输出这两个数组中的所有数据
* (先交替输出,如果奇数个数多,则再输出剩下的奇数,如果偶数个数多,则再输出剩下的偶数)。
* (提示与要求:
* (1)定义一个数组存储从键盘输入的8个整数,先判断这8个整数中奇数和偶数的个数,才能定义存储奇数和偶数的数组的长度;
* (2)把一个大的数组分别存放在奇数和偶数数组中并交替输出的过程定义为方法)
* <p>
* 3、定义一个数组,数组成员10个,找出数组中最大数连同下标一起输出
* <p>
* <p>
* 4、给定一个整型数组,数组成员10个,求该数组中第二大的数的下标
* <p>
* 5、B哥去参加青年歌手大奖赛,有10个评委打分,(去掉一个最高一个最低)求平均分?
* <p>
* 6、定义数组,存放5个学生的成绩【成绩值自己设定】,将成绩从大到小排序,获得成绩之和,平均成绩,最小成绩,最大成绩。
* <p>
* <p>
* 7、提取一个方法,将指定数组中的数组元素进行反转
* 例如:{10,23,2,45,6}—>{6,45,2,23,10}
* <p>
* 8、现在有如下的一个数组:
* int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ;
* 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:
* int[] newArr = {1,3,4,5,6,6,5,4,7,6,7,5} ;
* <p>
* 9、现在给出两个数组:
* 数组a:"1,7,9,11,13,15,17,19"
* 数组b:"2,4,6,8,10"
* 两个数组合并为数组c。
* <p>
* 10、找到在第一个数组array1中出现,而在第二个数组array2中没有出现的数字。
*/
public class practice723 {
public static void main(String[] args) {
topic();
}
public static void topic() {
System.out.print("请输入你想运行题目的序号:");
Scanner s = new Scanner(System.in);
int t = s.nextInt();
switch (t) {
case 1:
topic1();
break;
case 2:
topic2();
break;
case 3:
topic3();
break;
case 4:
topic4();
break;
case 5:
topic5();
break;
case 6:
topic6();
break;
case 7:
topic7();
break;
case 70:
topic7X();
break;
case 8:
topic8();
break;
case 80:
topic8X();
break;
case 9:
topic9();
break;
case 10:
topic10();
break;
}
}
public static void topic1() {
int[] score = {72, 89, 65, 58, 87, 91, 53, 82, 71, 93, 76, 68};
int[] count = {0, 0, 0, 0, 0};
for (int i = 0; i < 12; i++) {
if (score[i] >= 90) {
count[4]++;
} else if (score[i] < 89 && score[i] >= 80) {
count[3]++;
} else if (score[i] < 79 && score[i] >= 70) {
count[2]++;
} else if (score[i] < 69 && score[i] >= 60) {
count[1]++;
} else {
count[0]++;
}
}
System.out.println("A级的人数:" + count[4]);
System.out.println("B级的人数:" + count[3]);
System.out.println("C级的人数:" + count[2]);
System.out.println("D级的人数:" + count[1]);
System.out.println("E级的人数:" + count[0]);
}
public static void topic2() {
int odd = 0;
int even = 0;
int[] a = new int[8];
for (int i = 0; i < a.length; i++) {
System.out.print("输入:");
Scanner s = new Scanner(System.in);
a[i] = s.nextInt();
}
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0) {
odd++;
} else {
even++;
}
}
int[] oddarray = new int[odd];
int[] evenarray = new int[even];
int p = 0, q = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0) {
oddarray[p] = a[i];
p++;
} else {
evenarray[q] = a[i];
q++;
}
}
if (evenarray.length > oddarray.length) {//如果偶数个数较多
for (int i = 0; i < oddarray.length; i++) {
System.out.print(oddarray[i] + " " + evenarray[i] + " ");
}//先按照奇数的个数交替输出
for (int i = oddarray.length; i < evenarray.length; i++) {
System.out.print(evenarray[i] + " ");
}//输出剩下的偶数
} else {
for (int i = 0; i < evenarray.length; i++) {
System.out.print(oddarray[i] + " " + evenarray[i] + " ");
}//先按照偶数的个数交替输出
for (int i = evenarray.length; i < oddarray.length; i++) {
System.out.print(oddarray[i] + " ");
}//输出剩下的奇数
}
}
public static void topic3() {
int[] a = new int[10];
Random random = new Random();
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(100);
}
int max = a[0];
int p = 0;
for (int i = 0; i < a.length; i++) {
if (max < a[i]) {
max = a[i];
p = i;
}
}
System.out.println("最大值为" + max + ",下标为" + p);
}
public static void topic4() {
int max = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE;
int[] a = new int[10];
Random random = new Random();
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(100);
if (a[i] > max) {
secondMax = max;
max = a[i];
} else if (a[i] > secondMax && a[i] != max) {
secondMax = a[i];
}
}
for (int i = 0; i < a.length; i++) {
if(a[i]==secondMax){
System.out.println("第二大的数下标为"+i);
}
}
}
public static void topic5() {
int[] a = new int[10];
Random random = new Random();
int max = 100;
int min = 60;
int sum = 0;
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(max) % (max - min + 1) + min;
}
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[i] >= a[i + 1]) {
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
System.out.println("最终平均分为" + ((sum - a[0] - a[a.length - 1]) / (a.length - 2)));
}
public static void topic6() {
int[] a = new int[5];
Random random = new Random();
int max = 100;
int min = 60;
int sum = 0;
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(max) % (max - min + 1) + min;
}
int maxscore = a[0];
int minscore = a[0];
for (int i = 0; i < a.length; i++) {
sum += a[i];
if (maxscore < a[i]) {
maxscore = a[i];
}
if (minscore > a[i]) {
minscore = a[i];
}
}
int avg = sum / a.length;
System.out.println("成绩之和:" + sum + "\n" + "平均成绩:" + avg + "\n" + "最小成绩:" + minscore + "\n" + "最大成绩:" + maxscore + "\n");
}
public static void topic7() {
int[] a = new int[5];
Random random = new Random();
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(100);
}
int[] b = Arrays.copyOf(a, a.length);
int p = a.length - 1;
for (int i = 0; i < a.length; i++) {
a[i] = b[p];
p--;
}
System.out.println("反转前:");
for (int i : b) {
System.out.print(i + " ");
}
System.out.println();
System.out.println("反转后:");
for (int i : a) {
System.out.print(i + " ");
}
}
public static void topic7X() {
int[] a = new int[5];
Random random = new Random();
for (int i = 0; i < a.length; i++) {
a[i] = random.nextInt(100);
}
System.out.println("反转前:");
for (int i : a) {
System.out.print(i + " ");
}
System.out.println();
System.out.println("反转后:");
for (int j = a.length - 1; j >= 0; j--) {//倒着遍历数组元素
System.out.print(a[j] + " ");
}
}
public static void topic8() {
int[] oldArr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
int n = 0;
for (int i = 0; i < oldArr.length; i++) {
if (oldArr[i] == 0) {
n++;
}
}
int[] newArr = new int[oldArr.length - n];
int j = 0;
for (int i = 0; i < oldArr.length; i++) {
if (oldArr[i] != 0) {
newArr[j] = oldArr[i];
j++;
}
}
for (int i : oldArr) {
System.out.print(i + " ");
}
System.out.println();
for (int i : newArr) {
System.out.print(i + " ");
}
}
public static void topic8X() {
int[] oldArr = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
int[] newArr = new int[oldArr.length];
int n = 0;
for (int i = 0; i < oldArr.length; i++) {
if (oldArr[i] != 0) {
newArr[n] = oldArr[i];
n++;
}
}
for (int i = 0; i < newArr.length; i++) {
if (newArr[i] != 0) {
System.out.print(newArr[i] + " ");
}
}
}
public static void topic9() {
int[] a = {1, 7, 9, 11, 13, 15, 17, 19};
int[] b = {2, 4, 6, 8, 10};
int[] c = new int[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
System.out.println(Arrays.toString(c));
}
public static void topic10() {
Random random = new Random();
int[] array1 = new int[10];
for (int i = 0; i < array1.length; i++) {
array1[i] = random.nextInt(10);
}
int[] array2 = new int[10];
for (int i = 0; i < array2.length; i++) {
array2[i] = random.nextInt(10);
}
int repeat = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array2.length; j++) {
if (array1[i] == array2[j]) {
repeat++;
break;
}
}
}
boolean flag = false;
int[] newarray = new int[array1.length - repeat];
int subscript = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array2.length; j++) {
if (array1[i] == array2[j]) {
flag = false;
break;
} else {
flag = true;
}
}
if (flag) {
newarray[subscript] = array1[i];
subscript++;
}
}
System.out.println(Arrays.toString(newarray));
}
}
2021-07-23 java练习题
最新推荐文章于 2024-04-30 15:14:23 发布