本文已同步至:村雨遥

实例 46

题目

两个字符串连贯程序。

剖析

要实现两个字符串的连贯有多种办法,其中最简略的就是利用 + 来实现。

实现

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

后果

实例 47

题目

读取 7 个数(1 - 50)的整数值,每读取一个值,就打印该值个数的 *

剖析

次要就是考验循环和打印的用法,难度不大。

实现

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++;        }    }}

后果

实例 48

题目

某公司采纳公用电话传递数据,数据是四位整数,在传递过程中是加密的,加密规定如下:每位数字都加上 5 而后用和除以 10 的余数来代替该数字,再将第一位和第四位替换,第二位和第三位替换。

剖析

实现起来很简略,只不过要把步骤离开:

  1. 首先输出四位数之后,将其个位、十位、百位、千位都合成进去;
  2. 而后将各位都加上 5,而后求和后除以 10 的余数代替各位上的数;
  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/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]);        }    }}

后果

实例 49

题目

计算字符串中子串呈现的次数。

剖析

别离输出两个字符串,而后利用 equals() 比照字符串中等同于子字符串的状况,呈现则次数加一,不过要留神的是当两个字符串均为空的时候,此时无奈比拟。

实现

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 + " 次!");    }}

后果

实例 50

题目

有五个学生,每个学生有 3 门课程问题,从键盘上输出数据(学号、姓名、三门课程问题),计算出均匀问题,并把原有数据和计算出的均匀分数寄存于磁盘中。

剖析

剖析题目,将性能逐个拆分,先是要定义一个二维数组来寄存五个学生的 6 个信息,而后别离输出五个学生的前 5 个信息,接着计算均匀问题,最初则是写入磁盘,值得注意的是,在读写文件时要留神流的敞开。

实现

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("数据已写入~");    }}

后果