2021-08-19

Java经典编程例题

45、判断一个素数能被几个9整除 

package test2;

import java.util.Scanner;

public class test45 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        int m = sc.nextInt();
        int count = 0;
        while (m % 9 == 0) {
            count++;
            m /= 9;
        }
        System.out.println(count++);
    }

}

 

 

46、两个字符串连接程序

import java.util.*;
public class test46 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("请输入一个字符串:");
        String str1 = s.nextLine();
        System.out.print("请再输入一个字符串:");
        String str2 = s.nextLine();
        String str = str1+str2;
        System.out.println("连接后的字符串是:"+str);
    }
}

分析:直接用加号就行

 

47、读取7个数(1—50)的整数值,每读取一个值,程序打印出该值个数的*。

import java.util.*;
public class lianxi47 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n=1,num;
        while(n<=7){
            do{
                System.out.print("请输入一个1--50之间的整数:");
                num=s.nextInt();
            }while(num<1||num>50);
            for(int i=1;i<=num;i++)
               {System.out.print("*");
            }
            System.out.println();
            n ++;
        }
    }
}

需要注意的点:最好加上对读取的数值的判断

 

 

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

package test2;

import java.util.Scanner;

public class test38 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
        int[] m = new int[4];
        while (true) {
            int s = sc.nextInt();
            if (s > 999 && s < 9999) {
                m[0] = s / 1000;
                m[1] = s / 100 % 10;
                m[2] = s / 10 % 10;
                m[3] = s % 10;
                break;
            } else {
                System.out.println("请输入四位数");
            }
        }
        for (int i = 0; i < 4; i++) {
            m[i] += 5;
            m[i] %= 10;
        }
        int temp;
        temp = m[0];
        m[0] = m[3];
        m[3] = temp;
        temp = m[1];
        m[1] = m[2];
        m[2] = temp;
        int n = m[0] * 1000 + m[1] * 100 + m[2] * 10 + m[3];
        System.out.println(n);
    }

}

分析:直接while即可
需要注意的点:需要判断输入的数字

 

49、计算字符串中子串出现的次数 分析:先输入字符串和子串,再将所有字符串中所有符合子串长度的子串放在String数组中,最后进行比较 需要注意的点: 因为不计算重叠的子串,所以跳过配对之后的部分拆分子串,需要再配对成功后改变i的值

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class test49 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        String str1 = r.readLine();
        String str2 = r.readLine();
        String[] m = new String[(str1.length() - str2.length() + 1)];
        int count = 0;
        for (int i = 0; i < m.length; i++) {
            m[i] = str1.substring(i, i + str2.length());
            if (m[i].equals(str2)) {
                count++;
                // 因为不计算重叠的子串,所以跳过配对之后的部分拆分子串
                i = i + str2.length();
            }
        }
        System.out.println(count);
    }

}

 



50、有五个学生,每个学生有3门课的成绩,从键盘输入以上数据(包括学生号,姓名,三门课成绩),计算出平均成绩,把原有的数据和计算出的平均分数存放在磁盘文件 "stud "中。

package test2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;

class Student {
    String num;
    String name;
    double[] score;
    double agv;

    Student() {
        num = null;
        name = null;
        score = new double[3];
        for (int i = 0; i < 3; i++) {
            score[i] = 0;
        }
    }

    Student(String num, String name, double s1, double s2, double s3) {//本题目没有要求
        this.num = num;
        this.name = null;
        score = new double[3];
        score[0] = s1;
        score[1] = s2;
        score[2] = s3;
    }

    double agv(double[] n) {
        double v = 0;
        for (int i = 0; i < n.length; i++) {
            v += n[i];
        }
        return v / n.length;
    }
}

public class test50 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Student[] student = new Student[5];
        ;
        for (int i = 0; i < 5; i++) {
            student[i] = new Student();
        }
        Scanner sc = new Scanner(System.in);
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        for (int i = 0; i < 5; i++) {
            System.out.println("请输入第" + (i + 1) + "位同学的成绩");
            student[i].num = sc.next();
            student[i].name = sc.next();
            student[i].score[0] = sc.nextDouble();
            student[i].score[1] = sc.nextDouble();
            student[i].score[2] = sc.nextDouble();
            student[i].agv = (student[i].score[0] + student[i].score[1] + student[i].score[2]) / 3;
        }
        try {
            BufferedWriter w = new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream("C:\\360Downloads\\stud")));
            for (int i = 0; i <5; i++) {
                w.write(student[i].num);
                w.write(student[i].name);
                w.write(String.valueOf(student[i].score[0]));//将double类型的分数转换为String类型写入
                w.write(String.valueOf(student[i].score[1]));//因为write方法不能写入double类型的数值
                w.write(String.valueOf(student[i].score[2]));
            }
            w.flush();
            w.close();
            System.out.println("ture");//成功保存输出ture
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

}

分析:定义一个学生类,包含学生的信息和成绩和一个计算平均成绩的方法

---------------------------------------------------------------------------------------

明天继续