普元智慧筆試總結
阿新 • • 發佈:2019-04-29
輸出內容 功能 scribe tin into final finally prepare try
筆試總共有四道題
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); } }
普元智慧筆試總結