标签:new 最小 java imp sys rate 数组 port print
1.有10个评委打分,(去掉一个最高一个最低)求平均分。
package a;
import java.util.*;
public class aa {
public static void main(String[] args) {
// TODO Auto-generated method stub;
int a[] = { 12,45,78,89,56,23,13,46,75,89};
int sum = 0;
System.out.println("评委们打分分别为:");
for (int i= 0 ;i<10;i++) {
System.out.print(a[i] + " ");
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
int temp = 0;
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
break;
}
}
}
a[0] = 0;
a[9] = 0;
for (int x = 0; x < 10; x++) {
sum = a[x] + sum;
}
sum = sum / 8;
System.out.println();
System.out.println("去掉一个最高分和最低分,平均分是" + sum);
}
}
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。
package a;
import java.util.*;
public class aa {
public static void main(String[] args) {
// TODO Auto-generated method stub;
Random r = new Random();
int[] a = new int[10];
System.out.println("随机生成数组为:");
for (int j = 0; j < a.length; j++) {
a[j] = r.nextInt(100);
System.out.print(a[j] + " ");
}
System.out.println();
int t = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
System.out.println("数组排序后为:");
for (int j = 0; j < 10; j++) {
System.out.print(a[j] + " ");
}
}
}
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)
package a;
import java.util.*;
public class aa {
public static void main(String[] args) {
// TODO Auto-generated method stub;
Random r = new Random();
int[] a = new int[7];
System.out.println("随机生成号码为:");
int t = 0;
fuck: for (int i = 0; i < a.length; i++) {
a[i] = r.nextInt(35) + 1;
t = a[i];
for (int j = 0; j < i; j++) {
if (a[j] == t) {
i--;
continue fuck;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
4.定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数
package a;
import java.util.*;
public class aa {
public static void main(String[] args) {
// TODO Auto-generated method stub;
Random r = new Random();
int[] a = new int[10];
System.out.println("随机生成数组为:");
for (int j = 0; j < a.length; j++) {
a[j] = r.nextInt(100);
System.out.print(a[j] + " ");
}
System.out.println();
int max = 0, min = 0, ji = 0, ou = 0;
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
for (int i = 1; i < a.length; i++) {
if (a[i] < min) {
min = a[i];
}
}
for (int i = 0; i < a.length; i++) {
if (a[i] % 2 == 0) {
ou += 1;
} else {
ji += 1;
}
}
System.out.println("最大值为" + max);
System.out.println("最小值为" + min);
System.out.println("偶数有" + ou + "个");
System.out.println("奇数有" + ji + "个");
}
}
标签:new 最小 java imp sys rate 数组 port print
原文地址:https://www.cnblogs.com/sigure0428/p/12711785.html