欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java_冒泡排序|获取数组最大值和最小值操作,编写函数得到0到n之间的随机数,并找出产生m个这样的随机数中最大的、最小的数,并统计其中>=60的有多少个

程序员文章站 2022-05-12 10:37:37
...
import java.util.Arrays;
import java.util.Scanner;

/**
 * 获取数组最大值和最小值操作:编写函数得到0到n之间的随机数。
 * 并找出产生m个这样的随机数中最大的、最小的数,并统计其中>=60的有多少个
 * 
 * @author Chill Lyn
 *
 */
public class MaxMinInArray {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Please input how many num do you want: ");
		int m = scanner.nextInt();
		System.out.println("Please input the upper limit: ");
		int n = scanner.nextInt();

		int[] a = new int[m];
		for (int i = 0; i < m; i++) {
			a[i] = (int) (Math.random() * n);
		}

		System.out.println(Arrays.toString(a));
		maxmin(a);
		largerThan60(a);

	}

	public static void maxmin(int[] a) {
		int temp = 0;
		boolean flag = true;

		for (int j = 0; j < a.length; j++) {

			for (int i = 0; i < a.length - 1 - j; i++) {
				if (a[i] > a[i + 1]) {
					temp = a[i];
					a[i] = a[i + 1];
					a[i + 1] = temp;
					flag = false;
				}
			}
			if (flag) {
				break;
			}
		}
		System.out.println("Max: " + a[a.length - 1]);
		System.out.println("Min: " + a[0]);

	}

	public static void largerThan60(int[] a) {
		int count = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] >= 60) {
				count++;
			}
		}
		System.out.println("Amount of larger than or equal to 60: " + count);
	}
}