You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pj26fkbs9 a3e54596dc
Update README.md
1 year ago
README.md Update README.md 1 year ago

README.md

package qimo;

import java.util.Scanner; /* 异常 try Catch , throws , throw */ public class Test01 { public static void main(String[] args) { int[] arr = new int[5]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < arr.length; i++) {//0<x<100 arr[i] = scanner.nextInt(); if(arr[i]<0||arr[i]>100){ try { throw new Exception("请输入合法数据:"); } catch (Exception e) { System.out.println(e.getMessage()); } } } //最大最小值 int max,min; max = min = arr[0];

    for (int i = 1; i < arr.length; i++) {
        if(arr[i]>max)
            max = arr[i];
        if(arr[i]<min)
            min = arr[i];
    }
    System.out.println(max);
    System.out.println(min);
    scanner.close();



















   /* int[] arr = new int[10];
    Scanner scanner = new Scanner(System.in);//范围0<x<100
    //循环
    for (int i = 0; i < arr.length; i++) {
        arr[i] = scanner.nextInt();
        if(arr[i]<0||arr[i]>100){
            try {
                throw new Exception("请输入合法数据:");
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
    //最大值、最小值
    int max,min;
    max = min = arr[0];
    for (int i = 1; i < arr.length; i++) {
        if(arr[i]>max)
            max = arr[i];
        if(arr[i]<min)
            min = arr[i];
    }
    System.out.println(max);
    System.out.println(min);
    scanner.close();*/
}

}

第二题: package qimo;

import java.io.Serializable;

/* 面向对象:封装、继承、多态 类的创建、构造方法的创建、接口(抽象方法)、实现 */ public class Test02 { public static void main(String[] args) { Student stu1 = new Student(); stu1.setName("111"); System.out.println(stu1.getName()); Student stu2 = new Student("112",18); System.out.println(stu2); } } interface Ifun{ //常量、抽象方法、默认方法、静态方法

void method1();

} class Cc implements Ifun{//extends

@Override
public void method1() {

}

}

class Student implements Comparable/implements Serializable/{ private static final long serialUID = 1l; private String name; private int age; //get、set方法

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

//有参构造

public Student(String name, int age) {
    this.name = name;
    this.age = age;
}

public Student() {
}

@Override
public String toString() {
    return "Student{" + "name='" + name + '\'' +
            ", age=" + age +
            '}';
}



@Override
public int compareTo(Student o) {
    if(this.getAge() - o.getAge() ==0)
        return this.getName().hashCode() - o.getName().hashCode();
    return this.getAge() - o.getAge();
}

} 第三题: package qimo;

import java.util.Comparator; import java.util.Iterator; import java.util.Set; import java.util.TreeMap;

/* 集合Collection、Map List、Set(TreeSet) TreeMap 遍历增强for、foreach、迭代器 学生信息Student:name,ageMap 班级信息 张三 18 物联241 王五 20 物联242 赵六 19 物联241 查找物联241的同学信息并且按照年龄逆序输出迭代器、Map实现。

/ public class Test03 { public static void main(String[] args) { Student s1 = new Student("张三",18); Student s2 = new Student("李四",20); Student s3 = new Student("王五",19); TreeMap<Student,String> tm = new TreeMap<>(new Comparator() { @Override public int compare(Student o1, Student o2) { return o2.getAge() - o1.getAge(); } }); tm.put(s1,"物联241"); tm.put(s2,"物联242"); tm.put(s3,"物联241"); //遍历 Set students = tm.keySet(); Iterator iterator = students.iterator(); while(iterator.hasNext()){ Student key = iterator.next(); String values = tm.get(key); if(values.equals("物联241")){ System.out.println(key+"="+values); } } } } 第四题: package qimo; / IO流 copy a.txt b.txt/字节流、字符流、缓冲流、转换流 对象序列化Student--a.txt */

import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner;

public class Test04 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] s = scanner.nextLine().split(" "); if(s[0].equals("copy")){ FileReader fr = null; FileWriter fw = null; try { fr = new FileReader(s[1]); fw = new FileWriter(s[2]); int c; while((c = fr.read())!=-1){ fw.write(c); } } catch (IOException e) { System.out.println(e.getMessage()); } finally { if(fr!=null){ try { fr.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } if(fw!=null){ try { fw.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } }} }

} 第五题: package qimo;

import java.io.*;

public class Test05 { public static void main(String[] args) throws IOException, ClassNotFoundException { write(); read(); } public static void read() throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt")); Object o = ois.readObject(); System.out.println(o); ois.close();

}
public static void write() throws IOException {
    Student student = new Student("张三",18);
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
    oos.writeObject(student);
    oos.close();

}

} 第六题: package qimo;

import java.sql.*;

public class Demo06 { public static void main(String[] args) { Statement stmt = null; ResultSet rs = null; Connection conn = null; try { // 1. 注册数据库的驱动 Class.forName("com.mysql.cj.jdbc.Driver"); // 2.通过DriverManager获取数据库连接 String url = "jdbc:mysql://localhost:3306/bbgu2024"; String username = "bbgu123"; //数据库用户名 String password = "12345678"; //数据库密码 conn = DriverManager.getConnection(url, username, password); // 3.通过Connection对象获取Statement对象 stmt = conn.createStatement(); // 4.使用Statement执行SQL语句。 String sql = "select count() from Studen"; rs = stmt.executeQuery(sql); if(rs.next()){ System.out.println(rs.getInt(1)); } // 5. 操作ResultSet结果集 / System.out.println("id | name | password| email | birthday"); while (rs.next()) { int id = rs.getInt("id"); // 通过列名获取指定字段的值 String name = rs.getString("name"); String psw = rs.getString("password"); String email = rs.getString("email"); Date birthday = rs.getDate("birthday"); System.out.println(id + " | " + name + " | " + psw + " | " + email + " | " + birthday); }*/ } catch (Exception e) { e.printStackTrace(); } finally { // 6.回收数据库资源 if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt = null; } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } } } }