本文已同步至:村雨遥

实例 61

题目

计算 m ~ n(m < n) 之间所有整数的和。

剖析

遍历 m ~ m 之间的所有整数,而后将他们进行叠加即可。

实现

import java.util.Scanner;/** * Created with IntelliJ IDEA. * * @author : zhangliang * @version : 1.0 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example61 * @createTime : 2021/9/15 16:17 * @email : 747731461@qq.com * @公众号 : 村雨遥 * @website : https://cunyu1943.github.io * @description : */public class Example61 {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.println("输出 m");        int m = scanner.nextInt();        System.out.println("输出 n");        int n = scanner.nextInt();        int sum = 0;        for (int i = m; i <= n; i++) {            sum += i;        }        System.out.println("sum = " + sum);    }}

后果

实例 62

题目

对随机生成的 10 个数进行首尾元素替换,而后升序排序后输入,最初在降序排序后输入。

剖析

生成随机数,次要用到 Random 类,而无论是首尾元素替换、升序排序还是降序排序,Java 中都有对应封装好的办法,咱们主须要调用即可。

实现

import java.util.ArrayList;import java.util.Collections;import java.util.Random;/** * Created with IntelliJ IDEA. * * @author : zhangliang * @version : 1.0 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example62 * @createTime : 2021/9/28 15:08 * @email : 747731461@qq.com * @公众号 : 村雨遥 * @website : https://cunyu1943.github.io * @description : */public class Example62 {    public static void main(String[] args) {        Random random = new Random();        ArrayList<Integer> integers = new ArrayList<>();        for (int i = 0; i < 10; i++) {            integers.add(random.nextInt());        }        System.out.println("生成的随机数组:" + integers);        Collections.swap(integers, 0, 9);        System.out.println("替换首尾元素后的数组" + integers);        Collections.sort(integers);        System.out.println("升序排列后的数组:" + integers);        Collections.reverse(integers);        System.out.println("降序排列后的数组:" + integers);    }}

后果

实例 63

题目

随机产生三个随机数 a,b,c,而后输入其最大值和最小值。

剖析

同样考查随机数的生成,而后对数组进行升序排序,排序后数组的第一个元素即为最小元素,最大元素即为最初一个元素。

实现

import java.util.Arrays;import java.util.Random;/** * Created with IntelliJ IDEA. * * @author : zhangliang * @version : 1.0 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example63 * @createTime : 2021/9/28 15:25 * @email : 747731461@qq.com * @公众号 : 村雨遥 * @website : https://cunyu1943.github.io * @description : */public class Example63 {    public static void main(String[] args) {        int[] arr = new int[3];        Random random = new Random();        for (int i = 0; i < arr.length; i++) {            arr[i] = random.nextInt();        }        System.out.println("生成的随机数组:" + Arrays.toString(arr));        Arrays.sort(arr);        System.out.println("最大的元素:" + arr[2]);        System.out.println("最小的元素:" + arr[0]);    }}

后果

实例 64

题目

输出一个百分制分数,而后输入该问题所属等级:

  • 0 ~ 59:fail;
  • 60 ~ 79:pass;
  • 80 ~ 89:good;
  • 90 ~ 100:excellent.

剖析

次要还是一个条件判断,这里应用 switch 进行判断即可。

实现

import java.util.Scanner;/** * Created with IntelliJ IDEA. * * @author : zhangliang * @version : 1.0 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example64 * @createTime : 2021/9/28 15:39 * @email : 747731461@qq.com * @公众号 : 村雨遥 * @website : https://cunyu1943.github.io * @description : */public class Example64 {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.println("请输出分数");        int score = scanner.nextInt();        System.out.println("输出的分数是:" + score);        switch (score / 10) {            case 0:                System.out.println("fail");                break;            case 1:                System.out.println("fail");                break;            case 2:                System.out.println("fail");                break;            case 3:                System.out.println("fail");                break;            case 4:                System.out.println("fail");                break;            case 5:                System.out.println("fail");                break;            case 6:                System.out.println("pass");                break;            case 7:                System.out.println("pass");                break;            case 8:                System.out.println("good");                break;            case 9:                System.out.println("excellent");                break;            case 10:                System.out.println("excellent");                break;            default:                break;        }    }}

后果

实例 65

题目

输入绝对值不大于 100 的随机整数,若生成的值为 50,那么就退出。

剖析

次要利用 while 循环直到生成的数是 50 时终止程序,而生成 100 内的随机整数只须要指定随机生成函数的范畴即可。

实现

import java.util.Random;/** * Created with IntelliJ IDEA. * * @author : zhangliang * @version : 1.0 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example65 * @createTime : 2021/9/28 15:33 * @email : 747731461@qq.com * @公众号 : 村雨遥 * @website : https://cunyu1943.github.io * @description : */public class Example65 {    public static void main(String[] args) {        int num = 0;        Random random = new Random();        do {            num = random.nextInt(100);            System.out.println("生成的随机数:" + num);        } while (num != 50);    }}

后果