…可以说是没什么意思… 直接看题吧… 1、计算一个航班的飞行时间,通过键盘输入两个数,分别代表航班起飞时间和到达时间(不用考虑跨天的情况)。比如一个航班起飞是7:30,到达是14:20,则输入730和1420,通过程序,则要求输出内容为:“航班飞行时间为6小时50分钟”。
import java.util.Scanner; //加减乘除直接搞定... public class test1 { public static void main(String[] args) { try { Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); int j = scanner.nextInt(); //取小时 int h1 = i / 100; int h2 = j / 100; //取分钟 int m1 = i % 100; int m2 = j % 100; if(h2 >= h1){ if(h2 == h1 && m2 >= m1){ if (m2 - m1 < 0){ int h = h2-h1-1;//小时差 int m = m2-m1+60;//分钟查 System.out.println("航班飞行时间为"+h+"小时"+m+"分钟"); }else { int h = h2-h1; int m = m2-m1; System.out.println("航班飞行时间为"+h+"小时"+m+"分钟"); } } else{ System.out.println("输入有误!"); } } else{ System.out.println("输入有误!"); } }catch (Exception e){ System.out.println("输入有误!"); } } }2.将一个Student对象使用jdbc保存到mysql数据库,要求写出数据库建表脚本以及写出使用jdbc插入数据库的java代码。 说明: Student包含三个属性(姓名、年龄、出生年月),如(张三、20、19860101) mysql数据库连接信息如下: driverclass: com.mysql.jdbc.Driver connection URL: jdbc:mysql://127.0.0.1:3306/test username:root password:000000
//JDBC连接数据库多种写法看你心情 import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class test2 { //写一个pojo对象 class Student{ private String name; private Integer age; private String birthday; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } //带参数构造 public Student(String name, Integer age, String birthday) { this.name = name; this.age = age; this.birthday = birthday; } } public void t() throws ClassNotFoundException, SQLException { try{ //1 加载数据库驱动 Class.forName("com.mysql.jdbc.Driver"); //2 获取数据库连接 String url = "jdbc:mysql://127.0.0.1:3306/test"; String user = "root" ; String password = "000000" ; Connection conn = DriverManager.getConnection(url, user, password); Student student = new Student("张三",20,"19860101"); //3 写sql语句 String sql = "insert into students (name,age,birthday) values(?,?,?)" ; PreparedStatement ps; try { ps = (PreparedStatement) conn.prepareStatement(sql); ps.setString(1, student.getName()); ps.setInt(2, student.getAge()); ps.setString(3, student.getBirthday()); ps.executeUpdate(); ps.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); }finally { conn.close(); } }catch (ClassNotFoundException e) { System.out.println("加载驱动失败!"); e.printStackTrace() ; } } }3.要求使用javax.servlet.Filter实现统计某个用户访问了多少次tomcat服务器的功能。其中用户已经登录,登录信息已经放到session,key是USER_ID,其中用户登录的访问请求不需要进行统计。
//没怎么明白...我就放了份之前写的登录用户统计放上去了4、使用4个子线程求出1到100的和并答应。每个子线程计算25个数
//1h考试时间...还有富余就封装了一下.. public class test4 { public static int sum = 0; public static Object LOCK = new Object(); public static void main(String[] args) throws InterruptedException { test4 add = new test4(); ThreadTest thread1 = add.new ThreadTest(1, 25);//第1个线程 ThreadTest thread2 = add.new ThreadTest(26, 50);//第2个线程 ThreadTest thread3 = add.new ThreadTest(51, 75);//第3个线程 ThreadTest thread4 = add.new ThreadTest(76, 100);//第4个线程 thread1.start(); thread2.start(); thread3.start(); thread4.start(); //调用join() thread1.join(); thread2.join(); thread3.join(); thread4.join(); System.out.println(sum); } //封装方法 //别问为啥没用Runable class ThreadTest extends Thread { private int begin; private int end; @Override public void run() { //sum加锁 synchronized (LOCK) { for (int i = begin; i <= end; i++) { sum += i; } } } public int getBegin() { return begin; } public void setBegin(int begin) { this.begin = begin; } public int getEnd() { return end; } public void setEnd(int end) { this.end = end; } //构造方法 public ThreadTest(int begin, int end) { this.begin = begin; this.end = end; } } }写的健壮性并不是很好…还有很多问题没有处理…