头图
This article has been synchronized to: Murakami Haruka

Example 46

topic

Two string concatenation programs.

analyze

There are many ways to achieve the connection of two strings, the easiest of which is to use + to achieve.

accomplish

 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/7 15:29
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example46
 * @description :
 */

public class Example46 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入一个字符串");
        String str1 = scanner.nextLine();
        System.out.println("再输入一个字符串");
        String str2 = scanner.nextLine();
        System.out.println("连接后的字符串为:" + str1 + str2);
    }
}

result

Example 47

topic

Read the integer value of 7 numbers (1 - 50), each time a value is read, print the number of the value * ;

analyze

It is mainly to test the usage of loop and printing, which is not difficult.

accomplish

 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/7 15:29
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example47
 * @description :
 */

public class Example47 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = 0;
        int count = 1;
        while (count <= 7) {
            do {
                System.out.println("输入一个 1 - 50 之间的整数");
                num = scanner.nextInt();
            } while (num < 1 || num > 50);
//            打印 * 号
            for (int i = 0; i < num; i++) {
                System.out.print("*");
            }
            System.out.println();
            count++;
        }
    }
}

result

Example 48

topic

A company uses a public telephone to transmit data. The data is a four-digit integer, which is encrypted during the transmission process. The encryption rules are as follows: add 5 to each digit, then replace the number with the remainder of dividing the sum by 10, and then add the first digit. The bit is swapped with the fourth bit, and the second bit with the third bit.

analyze

It's easy to implement, just separate the steps:

  1. After inputting the four digits first, decompose the ones, tens, hundreds and thousands.
  2. Then add 5 to each of them, and then divide the remainder by 10 after the sum to replace the number on each of them;
  3. Finally, the first and fourth bits are exchanged, and the second and third bits are exchanged;

accomplish

 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/7 15:29
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example48
 * @description :
 */

public class Example48 {
    public static int SIZE = 4;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入一个四位的整数");
        int num = scanner.nextInt();
        int[] arr = new int[SIZE];
//        千位
        arr[0] = num / 1000;
//        百位
        arr[1] = num % 1000 / 100;
//        十位
        arr[2] = num / 10 % 10;
//        个位
        arr[3] = num % 10;

//        每个数字都加上 5,然后除以 10 的余数代替
        for (int i = 0; i < SIZE; i++) {
            arr[i] += 5;
            arr[i] %= 10;
        }

//        交换 1,3 位,2,4 位
        for (int i = 0; i <= 1; i++) {
            int tmp = arr[i];
            arr[i] = arr[SIZE - 1 - i];
            arr[SIZE - 1 - i] = tmp;
        }

        System.out.println("加密后的数字");
        for (int i = 0; i < SIZE; i++) {
            System.out.print(arr[i]);
        }
    }
}

result

Example 49

topic

Counts the number of occurrences of a substring in a string.

analyze

Enter two strings respectively, and then use equals() to compare the equivalent substring in the string, and the number of occurrences will be increased by one, but it should be noted that when both strings are empty, No comparison is possible at this time.

accomplish

 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/7 15:29
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example49
 * @description :
 */

public class Example49 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("输入字符串");
        String str = scan.nextLine();
        System.out.println("输入子字符串");
        String subStr = scan.nextLine();
        // 计数
        int count = 0;

        if (str.equals("") || subStr.equals("")) {
            System.out.println("无输入字符串或子串,无法比较");
            System.exit(0);
        } else {
            // 对比字符串中出现子字符串,统计次数
            for (int i = 0; i < str.length() - subStr.length(); i++) {
                if (subStr.equals(str.substring(i, subStr.length() + i))) {
                    count++;
                }
            }
        }

        System.out.println("子串在字符串中出现 " + count + " 次!");
    }
}

result

Example 50

topic

There are five students, each student has 3 course grades, input data (student number, name, three course grades) from the keyboard, calculate the average grade, and store the original data and the calculated average grade on the disk middle.

analyze

Analyze the questions and split the functions one by one. First, define a two-dimensional array to store the 6 information of the five students, then enter the first 5 information of the five students, then calculate the average grade, and finally write to the disk. It is worth noting that when reading and writing files, pay attention to the closing of the stream.

accomplish

 import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
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/7 15:29
 * @project : Java 编程实例
 * @package : PACKAGE_NAME
 * @className : Example50
 * @description :
 */

public class Example50 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
//        存放 5 个学生的信息
        String[][] info = new String[5][6];
        for (int i = 0; i < info.length; i++) {
            System.out.println("输入第 " + (i + 1) + " 个学生的学号");
            info[i][0] = scanner.next();
            System.out.println("输入第 " + (i + 1) + " 个学生的姓名");
            info[i][1] = scanner.next();
            for (int j = 0; j < 3; j++) {
                System.out.println("输入第 " + (i + 1) + " 学生的第 " + (j + 1) + " 个成绩");
                info[i][j + 2] = scanner.next();
            }
        }

//        求平均分,并存入数组
        float avg = 0.0f;
        int sum = 0;
        for (int i = 0; i < 5; i++) {
            {
                sum = 0;
                for (int j = 2; j < 5; j++) {
                    sum += Integer.parseInt(info[i][j]);
                }
                avg = (float) sum / 3;
                info[i][5] = String.valueOf(avg);
            }
        }

//        写入磁盘
        String line = null;
        File file = new File("./student.txt");
        if (file.exists()) {
            System.out.println("文件已存在");
        } else {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try (BufferedWriter output = new BufferedWriter(new FileWriter(file))) {

            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 6; j++) {
                    line = info[i][j] + "\t";
                    output.write(line);
                }
                output.write("\n");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("数据已写入~");
    }
}

result


村雨遥
52 声望6 粉丝

我是 村雨遥,欢迎来到我的思否主页。