第九次作业

package ydy3;

import java.util.Scanner;

public class test3{

public static void main(String[] args) {

int[] score = new int[]{10, 20, 30, 40, 50, 60, 70, 80, 90, 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;
}
}
}
int sum = 0;
for (int i = 1; i < score.length - 1; i++)
{
sum += score[i];
}
System.out.println("平均分为:" + sum / (score.length - 2.0));
}
}

 

  2.自学一下Java随机数,生成一个长度为10的随机数组(每个数的范围是0~99),排序后输出。

package ydy3;

import java.util.Random;

public class test3{

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 ydy3;

import java.util.Random;

public class test3{

public static void main(String[] args) {

int[] score = new int[7];
Random r = new Random();
for (int i = 0; i < score.length; i++)
{
score[i] = r.nextInt(35) + 1;
}
System.out.println("35选7号码为:");
for (int i = 0; i < score.length; i++)
{
System.out.println(score[i]);
}
}
}

 

 4.定义一个长度为10的int数组(如果没有特殊说明,静态赋值动态赋值都可以),统计数组中的最大值、最小值、以及奇 数和偶数的个数

package ydy3;

import java.util.Random;

public class test3{

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);
}
System.out.println("原数组为:");
for (int i = 0; i < score.length; i++)
{
System.out.println(score[i]);
}
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;
}
}
}
int ji = 0, ou = 0;
for (int i = 0; i < score.length; i++)
{
if (score[i] % 2 == 0)
{
ou++;
}
else
{
ji++;
}
}
System.out.println("最小值为:" + score[0]);
System.out.println("最大值为:" + score[score.length - 1]);
System.out.println("奇数个数:" + ji);
System.out.println("偶数个数:" + ou);
}
}

 

posted @ 2020-04-16 11:59  大元128  阅读(118)  评论(0编辑  收藏  举报