1.有10个评委打分,(去掉一个最高一个最低)求平均分。
![复制代码](https://common.cnblogs.com/images/copycode.gif)
public class first {
public static void main(String[] args) {
int[] a = new int[]{1, 3, 2, 5, 8};
int t;
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j + 1] > a[j]) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
double sum=0;
for (int k = 1; k <a.length-1 ; k++) {
sum+=a[k];
}
System.out.println("平均分"+sum/(a.length-2));
}
}
2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。
package
chap1;
import
java.util.Random;
public
class
Main
{
public
static
void
main(String[] args)
{
int
[] score =
new
int
[
10
];
Random r =
new
Random();
for
(
int
i =
0
; i < score.length; i++)
{
score[i] = r.nextInt(
100
);
}
for
(
int
i =
0
; i < score.length -
1
; i++)
{
for
(
int
j =
0
; j < score.length -
1
- i; j++)
{
if
(score[j] > score[j +
1
])
{
int
tmp = score[j];
score[j] = score[j +
1
];
score[j +
1
] = tmp;
}
}
}
for
(
int
i =
0
; i < score.length; i++)
{
System.out.println(score[i]);
}
}
}
3.制作彩票35选7程序。 (就是1~35随机生成7个不重复的数)
package
a;
import
java.util.Random;
public
class
ma {
public
static
void
main(String[] args) {
int
a[] =
new
int
[
7
];
Random r =
new
Random();
for
(
int
i =
0
; i < a.length; i++) {
a[i] = r.nextInt(
35
)+
1
;
}
for
(
int
x =
0
; x < a.length; x++) {
System.out.println(a[x]);
}
}
}
4.定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数。
![复制代码](https://common.cnblogs.com/images/copycode.gif)
public class first {
public static void main(String[] args) {
int[] a = new int[]{2,3,52,3,69,58,46,12,53,27};
int max=a[0];
int mini=a[0];
int jishu=0;
int oushu=0;
for (int i = 1; i <a.length ; i++) {
if (a[i]>max){
max=a[i];
}
}
for (int j = 1; j <a.length ; j++) {
if (a[j]<mini){
mini=a[j];
}
}
for (int k = 0; k< a.length; k++) {
if (a[k]%2==0){
oushu+=1;
}else {
jishu+=1;
}
}
System.out.println("最大值为"+max+"最小值为"+mini+"偶数有"+oushu+"个"+"奇数有"+jishu+"个");
}
}