1. 程式人生 > >«面向對象程序設計(java)»第三周學習總結 周強 201771010141

«面向對象程序設計(java)»第三周學習總結 周強 201771010141

document lena 組成 交互界面 fileread scan 知識 姓名 href

實驗目的與要求

(1)進一步掌握Eclipse集成開發環境下java程序開發基本步驟;

(2)熟悉PTA平臺線上測試環境;

(3)掌握Java語言構造基本程序語法知識(ch1-ch3);

(4)利用已掌握Java語言基本程序設計知識,學習設計開發含有一個主類、類內可有多個方法的應用程序。

實驗內容和步驟

實驗1:采用個人賬號登錄https://pintia.cn/,使用邀請碼588329加入PTA平臺NWNU-2017NISE教學班(西北師範大學 計算機科學與工程學院 2017級 網絡與信息安全),完成《2018秋季西北師範大學面向對象程序設計(Java)(ch1-ch3)測試題1》,測試時間120分鐘;

實驗2-實驗3在課後完成

實驗2:公民身份證號碼按照GB11643—1999《公民身份證號碼》國家標準編制,由18位數字組成:前6位為行政區劃分代碼,第7位至14位為出生日期碼,第15位至17位為順序碼,第18位為校驗碼。從鍵盤輸入1個身份證號,將身份證號的年月日抽取出來,按年-月-日格式輸出。註意:輸入使用Scanner類的nextLine()方法,以免出錯。

輸入樣例:

34080019810819327X

輸出樣例:

1981-08-19

實驗的代碼如下:

import java.util.Scanner;
public class ID1 {
public static void main(String[] args)


{
System.out.println("請輸入身份證號18位!");
Scanner scanner=new Scanner(System.in);
String ID=scanner.nextLine();
while(ID.length()!=18)
{
System.out.println("身份證號碼必須是18位");
ID=scanner.nextLine();
}
int i=6;
String year = ID.substring(i,i+4);
String month=ID.substring(i+4,i+6);
String day=ID.substring(i+6,i+8);
System.out.println(year+"-"+month+"-"+day);

}
}

實驗結果:

技術分享圖片

2.studentfile.txt文件內容是本班同學的學號與姓名,利用此文件編制一個程序,將studentfile.txt文件的信息讀入到內存,並提供兩類查詢功能:(1)輸入姓名查詢學號;(2)輸入學號查詢姓名。要求程序具有友好人機交互界面。

編程建議:

(1)從文件中讀入學生信息,可以編寫如下函數:

public static void StudentsFromFile(String fileName))

(2)輸入姓名查找學生學號,可以編寫如下函數:

public static String findStudent(String name)

(3)輸入學號查找學生姓名,可以編寫如下函數:

public static String findStudent(String ID)

實驗代碼:

package d;

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 D {

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:\\Users\\周強\\Documents\\Tencent Files\\1260622021\\FileRecv\\實驗三\\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("結束!");

}

}

技術分享圖片

實驗總結:

通過幾次實驗本人對java這門學科真的不懂,只能盡自己最大努力去完成.對於Scanner類的使用還是不夠熟練,由於對於所學知識還沒有完全熟練的掌握,導致我對實驗三無法正確的輸出結果,我會繼續查找自己的錯誤。也會更加深入的學習,不斷地改正自己更加需要繼續加強對相關知識的學習。

«面向對象程序設計(java)»第三周學習總結 周強 201771010141