实例 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);}}
结果

