实例 26
题目
输入星期几的第一个字母来判断一下是星期几,若第一个字母一样,则继续判断第二个字母。
分析
输入第一个字母后进行判断,就可以区分出星期一、星期三、星期五,然后根据第二个输入的字母判断星期二和星期四的区别,星期六和星期天的区别。
实现
import java.util.Scanner;/*** Created with IntelliJ IDEA.** @author : cunyu* @version : 1.0* @email : 747731461@qq.com* @website : https://cunyu1943.github.io* @date : 2021/6/3 23:26* @project : Java 编程实例* @package : PACKAGE_NAME* @className : Example26* @description :*/public class Example26 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("输入首字母");char first = scanner.next().toUpperCase().charAt(0);char second;switch (first) {case 'M':System.out.println("星期一");break;case 'W':System.out.println("星期三");break;case 'F':System.out.println("星期五");break;case 'T':System.out.println("请输入第二个字母");second = scanner.next().toLowerCase().charAt(0);if (second == 'u') {System.out.println("星期二");} else if (second == 'h') {System.out.println("星期四");}break;case 'S':System.out.println("请输入第二个字母");second = scanner.next().toLowerCase().charAt(0);if (second == 'a') {System.out.println("星期六");} else if (second == 'u') {System.out.println("星期天");}break;default:break;}}}
结果

实例 27
题目
求 100 之内的素数
分析
设定一个标志位 flag,默认为 false 表示素数,一旦为 true,则表示该数不是一个素数,最后打印即可,此处由于每 5 个换行,所以多了一个 count 变量用于计数;
实现
/*** Created with IntelliJ IDEA.** @author : cunyu* @version : 1.0* @email : 747731461@qq.com* @website : https://cunyu1943.github.io* @date : 2021/6/4 10:48* @project : Java 编程实例* @package : PACKAGE_NAME* @className : Example27* @description :*/public class Example27 {public static void main(String[] args) {// 默认是素数boolean flag = false;int count = 0;System.out.println("100 内的素数:");for (int i = 2; i < 100; i++) {for (int j = 2; j <= Math.sqrt(i); j++) {// 能除尽,则表示不是素数,跳出当次内循环if (i % j == 0) {flag = true;break;} else {flag = false;}}// 如果是素数,则打印并计数,然后每行打印 5 个if (flag == false) {count++;System.out.print(i + "\t");if (count % 5 == 0) {System.out.println();}}}}}
结果

实例 28
题目
对 10 个数进行排序。
分析
可以用两者方法,将 10 个数放到数组之后,可以利用内置的 Arrays.sort() 方法进行排序,也可以用冒泡排序;
实现
import java.util.Arrays;import java.util.Scanner;/*** Created with IntelliJ IDEA.** @author : cunyu* @version : 1.0* @email : 747731461@qq.com* @website : https://cunyu1943.github.io* @date : 2021/6/4 10:57* @project : Java 编程实例* @package : PACKAGE_NAME* @className : Example28* @description :*/public class Example28 {public static void main(String[] args) {int[] arr = new int[10];Scanner scanner = new Scanner(System.in);for (int i = 0; i < 10; i++) {System.out.println("输入第 " + (i + 1) + " 个数");arr[i] = scanner.nextInt();}// 1、直接调用内置方法// Arrays.sort(arr);// System.out.println("内置方法排序后的数组:" + Arrays.toString(arr));// 2、冒泡for (int i = 0; i < 10; i++) {for (int j = i + 1; j < 10; j++) {if (arr[i] > arr[j]) {int tmp = arr[i];arr[i] = arr[j];arr[j] = tmp;}}}System.out.println("冒泡排序后:" + Arrays.toString(arr));}}
结果

实例 29
题目
求 3 * 3 矩阵对角线元素之和。
分析
定义一个二维数组来存放矩阵元素,然后将对角元素进行相加求和即可(对角线元素的一维和二维索引一样)。
实现
import java.util.Scanner;/*** Created with IntelliJ IDEA.** @author : cunyu* @version : 1.0* @email : 747731461@qq.com* @website : https://cunyu1943.github.io* @date : 2021/6/4 11:07* @project : Java 编程实例* @package : PACKAGE_NAME* @className : Example29* @description :*/public class Example29 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int[][] matrix = new int[3][3];System.out.println("输入矩阵元素(共 9 个)");for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {matrix[i][j] = scanner.nextInt();}}System.out.println("输入的矩阵为:");for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {System.out.print(matrix[i][j] + "\t\t");}System.out.println();}// 对角线元素之和int sum = 0;for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {if (i == j) {sum += matrix[i][j];}}}System.out.println("对角线元素之和:" + sum);}}
结果

实例 30
题目
有一个已经排好序的数组,现插入一个数,要求按原来的规律将其插入数组中。
分析
假设已经给定一个从小到大排好序的数组,要插入一个数,我们只需要将原数组元素复制到一个新的数组中,然后将要插入的数加入数组,对新的数组进行排序即可!
实现
import java.util.Arrays;import java.util.Scanner;/*** Created with IntelliJ IDEA.** @author : cunyu* @version : 1.0* @email : 747731461@qq.com* @website : https://cunyu1943.github.io* @date : 2021/6/4 11:16* @project : Java 编程实例* @package : PACKAGE_NAME* @className : Example30* @description :*/public class Example30 {public static void main(String[] args) {int[] arr = new int[]{1, 4, 12, 23, 43, 66};Scanner scanner = new Scanner(System.in);System.out.println("给定的数组为:" + Arrays.toString(arr));System.out.println("输入要插入的元素");int value = scanner.nextInt();int[] newArr = new int[arr.length + 1];for (int i = 0; i < arr.length; i++) {newArr[i] = arr[i];}// 赋值到新数组,然后排序newArr[arr.length] = value;Arrays.sort(newArr);System.out.println("插入元素后的数组为:" + Arrays.toString(newArr));}}
结果

