那些年,我们一起做过的 Java 课后练习题(61 - 65)
生活随笔
收集整理的這篇文章主要介紹了
那些年,我们一起做过的 Java 课后练习题(61 - 65)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實例 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);} }結果
總結
以上是生活随笔為你收集整理的那些年,我们一起做过的 Java 课后练习题(61 - 65)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu Linux开机黑屏的永久解
- 下一篇: 【水果大全】快看,你属于哪种水果身材?