本文已同步至:村雨遥

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

后果