JAVA實(shí)驗(yàn)5-流_第1頁(yè)
JAVA實(shí)驗(yàn)5-流_第2頁(yè)
JAVA實(shí)驗(yàn)5-流_第3頁(yè)
JAVA實(shí)驗(yàn)5-流_第4頁(yè)
JAVA實(shí)驗(yàn)5-流_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余13頁(yè)可下載查看

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、1. 編寫(xiě)程序,要求:用戶(hù)在鍵盤(pán)每輸入一行文本,程序?qū)⑦@段文本顯示在控制臺(tái)中。當(dāng)用戶(hù)輸入的一行文本是“exit ” (不區(qū)分大小寫(xiě))時(shí),程序?qū)⒂脩?hù)所有輸入的文本都寫(xiě)入到文件中,并退出。(要求:控制臺(tái)輸入通過(guò)流封裝獲取,不要使用Scanner)package shiyanwu1;importimportimportpublic class test1 quals(line)|"EXIT".equals(line) break;(line).append(LINE_SEP);esolveOrderDatas(dishesDatas);T T T件1件件1件錄文錄儀儀J 錄文目T

2、目T T件 EL TT 一一 一 一文if (temp20) != null) String dishesName = null;int dishesCount = 0;int totalPrice = 0;forString, Integere : () dishesName =();dishesCount =();totalPrice += (dishesName) * dishesCount;"總消費(fèi)為:"+totalPrice);private List<String> readFile(String fileName) if (fileName !=

3、null && !"".equals(fileName) File file = null;file = new File(fileName);if () List<String> datas = new ArrayList<String>();try Inputstream is = new FilelnputStream(file);BufferedReader br = new BufferedReader(new InputStreamReader(is,"gb2312");String str = nul

4、l;while (true) str = ();if (str != null) (str); else break;(); catch (Exception e) return datas;return null;private Map<String, Integer> resolveOrderDatas(List<String> datas) String temp1 = null, temp2 = null;String detailStr = null;Map<String, Integer> orderDetail = new HashMap<

5、;>();for (int i = 0; i < (); i+) temp1 = (i).split(",");for (int j = 0; j < ; j+) temp2 = temp1j.split(":");if = 2) (temp20, (temp21)+ (temp20); else (temp20, (temp21);return orderDetail;private Map<String, Integer> resolveDishesDatas(List<String> datas) Map&

6、lt;String, Integer> dishesDetail = new HashMap<>();String temp = null;for (int i = 0; i < (); i+) temp = (i).split(":");if = 2) (temp0, (temp1);return dishesDetail;2. 設(shè)計(jì)學(xué)生類(lèi)Student ,屬性:學(xué)號(hào)(整型);姓名(字符串),選修課程(名稱(chēng))及課程成績(jī)(整型)。編寫(xiě)一個(gè)控制臺(tái)程序,能夠?qū)崿F(xiàn)Student 信息的保存、讀取。具體要求:(1)提供 Student 信息的保存功能:通過(guò)控

7、制臺(tái)輸入若干個(gè)學(xué)生的學(xué)號(hào)、姓名以及每個(gè)學(xué)生所修課程的課程名和成績(jī),將其信息保存到中;(2)數(shù)據(jù)讀取顯示:能夠從文件中讀取學(xué)生及其課程成績(jī)并顯示于控制臺(tái)。package shiyanwu4;public class Student private int number;private String name;private String courseName;private int score;public int getNumber() return number;public void setNumber(int number) = number;public String getName(

8、) return name;public void setName(String name) = name;public String getCourseName() return courseName;public void setCourseName(String courseName) = courseName;public int getScore() return score;public void setScore(int score) =score;package shiyanwu4;import class StudentMgr public static final Stri

9、ng studentDat = "E:/"public static void main(String口 args) showMenu();Scanner s = null;s = new Scanner;String code = null;Student student;List<Student> datas = new ArrayList<Student>();List<Student> savedStudents = readStudentDat(studentDat); while (true) code =();if (&qu

10、ot;#4".equalsIgnoreCase(code) ”程序已退出");break; else if ("#1".equalsIgnoreCase(code) String tmpStr = null;int tmpInt;while (true) " 學(xué)生學(xué)號(hào):");tmpInt = ();student = new Student();(tmpInt);" 學(xué)生姓名:");tmpStr = ();(tmpStr);" 學(xué)生課程:");tmpStr = ();(tmpStr);"

11、; 課程成績(jī):");tmpInt = ();(tmpInt);(student);" 輸入 exit 結(jié)束信息錄入, 輸入其他繼續(xù)錄入");tmpStr = ();if ("exit".equalsIgnoreCase(tmpStr) break;if ("exit".equalsIgnoreCase(tmpStr) showMenu();continue; else if ("#3".equalsIgnoreCase(code) try if() >0 )saveStudents(datas);e

12、lse" 無(wú)可保存的學(xué)生信息"); catch (IOException e) " 保存學(xué)生信息異常");();else if ("#2".equalsIgnoreCase(code) List<Student> students = readStudentDat(studentDat);if(students = null | () = 0)" 暫無(wú)學(xué)生信息");showMenu();else" 已有學(xué)生人數(shù):"+();for(int i=0;i<();i+)quals(fi

13、leName) File file = null;file = new File(fileName);Student student = null;if () List<Student> datas = new ArrayList<Student>();try InputStream is = new FileInputStream(file);BufferedReader br = new BufferedReader(new InputStreamReader(is, "gb2312");String str = null;String info

14、s = null;while (true) str = ();if (str != null) student = new Student();str = ();infos = ("#");(infos0);(infos1);(infos2);(infos3);(student); else break;(); catch (Exception e) ();return datas;return null;public static void saveStudents(List<Student> students) throws IOException File

15、 file = new File(studentDat);if (!() ();BufferedWriter bw = new BufferedWriter(new FileWriter(file,true);StringBuffer sb = new StringBuffer();Student s = null;for (int i = 0; i < (); i+) s = (i);(0);() + "#" + () + "#"+ () + "#" + ();();("n");();();public s

16、tatic void showMenu() "");#1、錄入學(xué)生信息");#2、查看學(xué)生信息");"#3 、保存學(xué)生信息");"#4 、退出 ");"");5. 編寫(xiě)程序,在控制臺(tái)窗口提示輸入兩個(gè)整數(shù),然后接收這兩個(gè)整數(shù),并輸出它們的和。(要求:鍵盤(pán)輸入通過(guò)流封裝獲取,不要使用Scanner 類(lèi))package class InputStreamTest public static void main(String args) BufferedReader in = new BufferedR

17、eader(new InputStreamReader);String line=null;try" 請(qǐng)輸入第一個(gè)正整數(shù)a: ");line = ();int a = (line);" 請(qǐng)輸入第二個(gè)正整數(shù)b: ");line = ();int b = (line);"a+b=" + (a+b);catch(Exception e)" 輸入錯(cuò)誤!");(0);1)輸入3個(gè)學(xué)生的姓6.設(shè)計(jì)學(xué)生類(lèi)Student ,屬性:編號(hào)(整型);姓名(字符串),成績(jī)(整型)。編寫(xiě)一個(gè)程序:要求:(名和成績(jī),將其姓名和成績(jī)保存到中;(

18、2)然后從該文件中讀取數(shù)據(jù),求得這五個(gè)學(xué)生的平均成績(jī)package class Student int id;String name;int score;Student()public void setId(int id)=id;public void setName(String name)=name;public void setScore(int score)=score;public String toString()return + "t" + + "t" + + ”n"package class Main public static void main(String口 args) BufferedReader in = new BufferedReader(new InputStreamReader);tryDataOutputStream out = new DataOutputStream(new FileOutpu

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論