笔试总共有四道题
1、计算一个航班的飞行时间,通过键盘输入两个数,分别代表航班起飞时间和到达时间(不用考虑跨天的情况)。比如一个航班起飞是7:30,到达是14:20,则输入730和1420,通过程序,则要求输出内容为:“航班飞行时间为6小时50分钟”。
这道题猛一眼看上去准备用Java的calendar来做,但是转念一想,简单减法也可以
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
Integer i = scanner.nextInt();
Integer j = scanner.nextInt();
//小时
Integer h1 = i / 100;
Integer h2 = j / 100;
//分钟
Integer m1 = i % 100;
Integer m2 = j % 100;
if (m2 - m1 < 0){
Integer h = h2-h1-1;
Integer m = m2-m1+60;
System.out.println(“航班飞行时间为”+h+“小时”+m+“分钟”);
}else {
Integer h = h2-h1;
Integer m = m2-m1;
System.out.println(“航班飞行时间为”+h+“小时”+m+“分钟”);
}
}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插入操作
package test;
import java.sql.*;
/**
@author zsh
@company wlgzs
@create 2019-03-21 20:19
@Describe JDBCTest,PreparedStatement与Statement区别
*/
public class JDBCTest {
class Student{
private String name;
private Integer age;
private String birthday;
public Student(String name, Integer age, String birthday) {
this.name = name;
this.age = age;
this.birthday = 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 void t() throws ClassNotFoundException, SQLException {
//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 创建一个Statement
String sql = “insert into students (name,age,birthday) values(?,?,?)” ;
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, student.getName());
pstmt.setInt(2, student.getAge());
pstmt.setString(3, student.getBirthday());
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
conn.close();
}
}
}
3、要求使用javax.servlet.Filter实现统计某个用户访问了多少次tomcat服务器的功能。其中用户已经登录,登录信息已经放到session,key是USER_ID,其中用户登录的访问请求不需要进行统计。
这个题有点小疑问,
import java.io.;
import javax.servlet.;
import javax.servlet.http.;
import java.util.;
public class TestFilter implements Filter{
private int count;
public void init(FilterConfig config)
throws ServletException{
// Reset hit counter.
count = 0;
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws java.io.IOException, ServletException {
HttpSession session = request.getSession(false);
User user = session.getAttribute(“USER_ID”);
if (user != null){
count++;
}
chain.doFilter(request,response);
}
public void destroy() {
System.out.println(“访问次数 :”+ count );
count = 0;
}
}
4、使用4个子线程求出1到100的和并答应。每个子线程计算25个数
这道题多个解法,我这里使用最简单的,开四个线程去执行
package test;
/**
@author zsh
@site qqzsh.top
@company wlgzs
@create 2019-04-29 19:56
@Description
*/
public class FourThread {
static int sum11 = 0;
static int sum12 = 0;
static int sum13 = 0;
static int sum14 = 0;
static int sum = 0;
public static void main(String[] args) throws InterruptedException {
new Thread(){
public void run() {
int sum1=0;
for(int i=1;i<=25;i++){
sum1+=i;
}
sum11 += sum1;
}
}.start();
new Thread(){
public void run() {
int sum2 = 0;
for (int i = 26; i <= 50; i++) {
sum2 += i;
}
sum12 += sum2;
}
}.start();
new Thread(){
public void run() {
int sum3 = 0;
for (int i = 51; i <= 75; i++) {
sum3 += i;
}
sum13 += sum3;
}
}.start();
new Thread(){
public void run() {
int sum4 = 0;
for (int i = 76; i <= 100; i++) {
sum4 += i ;
}
sum14 = sum4;
}
}.start();
Thread.sleep(100);
sum = sum11 + sum12 + sum13 + sum14;
System.out.println( "sum: " + sum);
}
}
相关文章
推荐阅读
●Java构架师之路
●计算机教程汇总
●Java面试大全
●网站推荐
●软件推荐
●点击加入QQ群874514813交流学习,或获取更多资料