焦旭超201771010109《面向對象程序設計(java)》第三周學習總結
理論知識學習部分
第一章:簡單了解java語言的程序設計平臺及java的優劣。
第二章:安裝java開發工具包,使用命令行工具,使用集成開發環境,進行簡單的java程序設計。
第三章:3.1-3.7 了解註釋類型,數據類型,變量,運算符,字符串,輸入輸出(讀取輸入,格式化輸出,文件輸入與輸出)。
3.8 五種語句:控制語句:if,switch(分支),for while,do while(循環)
方法調用語句:system.out.pritln(“...”)
表達式語句:x=23;i++;
復合語句:{z=x+3;ststem.out...}用{}括起來
package語句和import語句
3.9 當基本的整數和浮點數精度不能滿足需求是,可以使用java.math包中的兩種類:BigInteger和BigDecimal。
3.10 數組名字=new 數組元素類型【個數】
如:boy=new float[5]
int[] age=new int[10]
for each 循環語句的循環變量會歷遍數組中每個元素,而不需要使用下標值。
實驗部分
實驗2:
公民身份證號碼按照GB11643—1999《公民身份證號碼》國家標準編制,由18位數字組成:前6位為行政區劃分代碼,第7位至14位為出生日期碼,第15位至17位為順序碼,第18位為校驗碼。從鍵盤輸入1個身份證號,將身份證號的年月日抽取出來,按年
輸入樣例:
34080019810819327X
輸出樣例:
1981-08-19
ps:本實驗做字符串截取
實驗3:
studentfile.txt文件內容是本班同學的學號與姓名,利用此文件編制一個程序,將studentfile.txt文件的信息讀入到內存,並提供兩類查詢功能:
(1)輸入姓名查詢學號;(2)輸入學號查詢姓名。
要求程序具有友好人機交互界面。
(1)從文件中讀入學生信息,可以編寫如下函數:
public static void StudentsFromFile(String fileName))
(
public static String findStudent(String name)
(3)輸入學號查找學生姓名,可以編寫如下函數:
public static String findStudent(String ID)
代碼如下:
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class Main { private static ArrayList<Student>studentList = null; public static void StudentsFromFile(String fileName){ File file = new File(fileName); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; while ((tempString = reader.readLine()) != null) { String str[] = tempString.split(" "); if(studentList!= null && str.length > 1) { //Object studentId; Student student = new Student(); student.setStudentId(str[0]); student.setName(str[1]); studentList.add(student); } } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } } public static String findStudentIdByName(String name) { String studentId = null; for(Student student : studentList) { if(student.getName().equals(name)) { studentId = student.getStudentId(); break; } } return studentId; } public static String findStudentNameById(String ID) { String studentName = null; for(Student student : studentList) { if(student.getStudentId().equals(ID)) { studentName = student.getName(); break; } } return studentName; } public static void main(String args[]) { String path = "C:\\studentfile.txt"; studentList = new ArrayList<Student>(); StudentsFromFile(path); int statu = 1; System.out.println(); while(statu != 0) { System.out.println("******************"); System.out.println("1:輸入姓名查詢同學的信息"); System.out.println("2:輸入學號查詢同學的信息"); System.out.println("0:返回"); System.out.println("******************"); Scanner in = new Scanner(System.in); statu = in.nextInt(); switch(statu) { case 1:{ System.out.println("請輸入學生姓名:"); Scanner scanner1 = new Scanner(System.in); String name = scanner1.nextLine(); String Id = findStudentIdByName(name); if(Id != null) { System.out.println("姓名: "+name+" 學號: "+Id); }else { System.out.println("無此學生,請確認"); } }break; case 2:{ System.out.println("請輸入學生學號:"); Scanner scanner2 = new Scanner(System.in); String Id = scanner2.nextLine(); String name = findStudentNameById(Id); if(name != null) { System.out.println("姓名: "+name+" 學號: "+Id); }else { System.out.println("無此學生,請確認"); } }break; case 0: statu = 0; break; default: System.out.println("輸入錯誤"); } } System.out.println("結束!"); } }
實驗總結
這一周學習的理論知識不多,但都比較重要,流程控制語句如:條件語句,循環語句等相關語句的知識,和大數值的和數組的知識。實驗三用到了前面學習的文件讀寫等內容,難度比較大,所以請教了其他同學。通過本次實驗我知道前面學習的知識都沒有掌握。接下來還要繼續努力。
焦旭超201771010109《面向對象程序設計(java)》第三周學習總結