Java程序設(shè)計課程設(shè)計猜數(shù)游戲_第1頁
Java程序設(shè)計課程設(shè)計猜數(shù)游戲_第2頁
Java程序設(shè)計課程設(shè)計猜數(shù)游戲_第3頁
Java程序設(shè)計課程設(shè)計猜數(shù)游戲_第4頁
Java程序設(shè)計課程設(shè)計猜數(shù)游戲_第5頁
已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、課程設(shè)計(論文)題 目 名 稱 猜數(shù)游戲 課 程 名 稱 java 程序設(shè)計課程設(shè)計 學 生 姓 名 學 號 系 、專 業(yè) 信息工程系、07網(wǎng)絡(luò)工程專業(yè) 指 導(dǎo) 教 師 2010年 1 月 3 日摘 要 本程序是一個游戲程序,程序讓計算機自動生成一個隨機數(shù),讓玩家猜,猜的數(shù)比隨機數(shù)大則提示“大了”,比隨機數(shù)小則提示“小了”,再讓玩家重新猜,直到猜對為止,猜對了以后會表揚玩家,并算出玩家共猜了多少次。關(guān)鍵詞: 游戲、隨機數(shù)、猜數(shù)目 錄1 問題描述.12 需求分析.13 概要設(shè)計.13.1 自定義類說明.13.2 程序流程分析 . 14 詳細設(shè)計.44.1 game類的實現(xiàn).45 測試分析.66

2、課程設(shè)計總結(jié).8參考文獻9附錄(部分源程序清單)101 問題描述計算機產(chǎn)生隨機數(shù),猜中即勝,猜不中,提示是大了還是小了,繼續(xù)猜,直至猜到,給出所用次數(shù)和評語。2 需求分析 需要創(chuàng)建一個game類,該類實現(xiàn)產(chǎn)生隨機數(shù)、提示出錯信息、計算猜錯次數(shù)、給出玩家游戲信息等功能。其中包括一個循環(huán)函數(shù),玩家猜的不對就提示“大了”或“小了”要求玩家再次輸入,不斷循環(huán),直到玩家猜對。還包括提示出錯程序,即玩家輸入不是數(shù)字或不輸入時會提示出錯。還有計數(shù)函數(shù),計算玩家共猜了多少次。最后是猜對表揚函數(shù),即玩家猜對后表揚玩家,同時給出玩家共猜了多少次的信息。3 概要設(shè)計31自定義類說明3.1.1類名: game類(1)

3、作用: 主類,用來(2)繼承的父類: 沒有(3)實現(xiàn)的接口: 沒有 (4)成員變量說明: 整型變量rnd:計算機產(chǎn)生的隨機數(shù) 整型變量counter:存放猜數(shù)次數(shù)的變量 字符串變量str:存放輸入字符串(5)成員方法說明: math.random():隨機產(chǎn)生一個數(shù) joptionpane.showinputdialog:產(chǎn)生一個輸入消息對話框 joptionpane.showmessagedialog:產(chǎn)生一個輸出消息對話框 integer.parseint:強制類型轉(zhuǎn)換32 程序流程分析開始顯示“小了”i<rnd不能為空,錯誤輸入錯誤輸入!inti=integer.parseint(

4、str);str.equals("")不是數(shù)字c+c<sarray.lengthintc=0true計算機產(chǎn)生隨機數(shù)請輸入數(shù)據(jù),并賦值給stri>rndcounter>5顯示“大了”成功,你只猜了counter次,你真是太棒了 恭喜你猜對了,你猜了counter次import java.awt.event.*;import javax.swing.*;import java.io.*;import java.security.*;import javax.crypto.*;import javax.crypto.spec.*;public class fi

5、leencryptert extends jframe package key;final jbutton jbe=new jbutton("加密");final jbutton jbd=new jbutton("解密");fileencryptert()super("文件加密器(trides)");setbounds(100,100,600,380);this.setresizable(false);container c=getcontentpane();c.setlayout(new flowlayout();final fil

6、epanel fp = new filepanel("文件選擇");c.add(fp);final keypanel pp=new keypanel("輸入key");c.add(pp);jbe.addactionlistener(new actionlistener()public void actionperformed(actionevent event)file file = new file(fp.getfilename();if (file.exists()encrypt(file.getabsolutefile(),pp.getkey();

7、elsejoptionpane.showmessagedialog( null,"請選擇文件!","提示",joptionpane.ok_option););jbd.addactionlistener(new actionlistener()public void actionperformed(actionevent event)file file = new file(fp.getfilename();if (file.exists()decrypt(file.getabsolutefile(),pp.getkey();elsejoptionpane

8、.showmessagedialog(null,"請選擇文件!","提示",joptionpane.ok_option););getcontentpane().add(jbe);getcontentpane().add(jbd);public static void main(string args) fileencryptert fe=new fileencryptert();fe.show();private void encrypt(file filein,string skey)try if(skey.length() = 48) byte by

9、tk1 = getkeybystr(skey.substring(0,16); byte bytk2 = getkeybystr(skey.substring(16,32); byte bytk3 = getkeybystr(skey.substring(32,48); fileinputstream fis = new fileinputstream(filein); byte bytin = new byte(int)filein.length(); for(int i = 0;i<filein.length();i+) bytini = (byte)fis.read(); byte

10、 bytout = encryptbydes(encryptbydes( encryptbydes(bytin,bytk1),bytk2),bytk3); string fileout = filein.getpath() + ".tdes" fileoutputstream fos = new fileoutputstream(fileout); for(int i = 0;i<bytout.length;i+) fos.write(int)bytouti);fos.close();joptionpane.showmessagedialog(this,"加

11、密成功!","提示",joptionpane.ok_option);elsejoptionpane.showmessagedialog(this,“密碼長度必須等于48!”,“錯誤信息”joptionpane.error_message); catch(exception e)e.printstacktrace();private void decrypt(file filein,string skey)try if(skey.length() = 48) string strpath = filein.getpath();if(strpath.substring

12、(strpath.length()-5).tolowercase().equals(".tdes") strpath = strpath.substring(0,strpath.length()-5);else joptionpane.showmessagedialog(this,"不是合法的加密文件!","提示",joptionpane.ok_option);return;jfilechooser chooser = new jfilechooser();chooser.setcurrentdirectory(new file(&q

13、uot;.");chooser.setselectedfile(new file(strpath);int ret = chooser.showsavedialog(this);if(ret=jfilechooser.approve_option)byte bytk1 = getkeybystr(skey.substring(0,16);byte bytk2 = getkeybystr(skey.substring(16,32);byte bytk3 = getkeybystr(skey.substring(32,48);fileinputstream fis = new filei

14、nputstream(filein);byte bytin = new byte(int)filein.length();for(int i = 0;i<filein.length();i+)bytini = (byte)fis.read();byte bytout = decryptbydes(decryptbydes(decryptbydes(bytin,bytk3),bytk2),bytk1); file fileout = chooser.getselectedfile();fileout.createnewfile();fileoutputstream fos = new fi

15、leoutputstream(fileout);for(int i = 0;i<bytout.length;i+)fos.write(int)bytouti);fos.close();joptionpane.showmessagedialog(this,"解密成功!","提示",joptionpane.ok_option);elsejoptionpane.showmessagedialog(this,"密碼長度必須等于48!","錯誤信息 ",joptionpane.error_message);catch(

16、exception e)joptionpane.showmessagedialog(this,"解密失敗,請核對密碼!","提示",joptionpane.ok_option);private byte encryptbydes(byte bytp,byte bytkey) throws exception deskeyspec desks = new deskeyspec(bytkey); secretkeyfactory skf = secretkeyfactory.getinstance("des"); secretkey sk

17、 = skf.generatesecret(desks); cipher cip = cipher.getinstance("des"); cip.init(cipher.encrypt_mode,sk); return cip.dofinal(bytp);private byte decryptbydes(byte byte,byte bytkey) throws exception deskeyspec desks = new deskeyspec(bytkey); secretkeyfactory skf = secretkeyfactory.getinstance(

18、"des"); secretkey sk = skf.generatesecret(desks); cipher cip = cipher.getinstance("des"); cip.init(cipher.decrypt_mode,sk); return cip.dofinal(byte);private byte getkeybystr(string str)byte bret = new bytestr.length()/2;for(int i=0;i<str.length()/2;i+)integer itg= newinteger(1

19、6*getchrint(str.charat(2*i)getchrint(str.charat(2*i+1);breti = itg.bytevalue();return bret;private int getchrint(char chr)int iret=0;if(chr="0".charat(0) iret = 0;if(chr="1".charat(0) iret = 1;if(chr="2".charat(0) iret = 2;if(chr="3".charat(0) iret = 3;if(chr=

20、"4".charat(0) iret = 4;if(chr="5".charat(0) iret = 5;if(chr="6".charat(0) iret = 6;if(chr="7".charat(0) iret = 7;if(chr="8".charat(0) iret = 8;if(chr="9".charat(0) iret = 9;if(chr="a".charat(0) iret = 10;if(chr="b".chara

21、t(0) iret = 11;if(chr="c".charat(0) iret = 12;if(chr="d".charat(0) iret = 13;if(chr="e".charat(0) iret = 14;if(chr="f".charat(0) iret = 15;return iret; system.exit(0);counter+;結(jié)束 圖3.1 主程序流程圖程序運行時計算機產(chǎn)生一個隨機數(shù),并把這個隨機數(shù)賦值給rnd,再進入while循環(huán),程序要求玩家輸入一個數(shù)字,不為數(shù)字或輸入為空時,彈出對話框

22、,提示輸入為空和錯誤輸入。當輸入的數(shù)比隨機數(shù)小時彈出對話框提示“小了”,當輸入的數(shù)比隨機數(shù)大時彈出對話框提示“大了”,要求玩家再次輸入數(shù)字直到輸入的數(shù)和隨機數(shù)一樣時退出循環(huán),并彈出成功對話框,給出評語“恭喜你猜對了,你猜了counter 次”或“你只猜了 counter 次,你真是太棒了!”如圖3.1所示。4 詳細設(shè)計41game類的實現(xiàn)/創(chuàng)建game類public class game public static void main(string args) /rnd是計算機產(chǎn)生的隨機數(shù) int rnd=(int)(math.random()*100)+1; /counter用來統(tǒng)計玩家猜數(shù)

23、次數(shù) int counter=1; while(true) /輸入對話框,用showinputdialog方法 string str=javax.swing.joptionpane.showinputdialog("請輸入數(shù)據(jù):"); /定義一個字符數(shù)組,將str轉(zhuǎn)換后的值賦給它 char sarray =str.tochararray(); for(int c=0;c<sarray.length;c+) /判斷是否為數(shù)字,若不是則提示錯誤 if(!character.isdigit(sarrayc) javax.swing.joptionpane.showmessa

24、gedialog(null,"錯誤輸入!"); system.exit(0); /判斷是否空if (str.equals("") /單純顯示信息,用showmessagedialog方法 joptionpane.showmessagedialog(null, "不能為空!","錯誤", joptionpane.error_message); /把str的值強制轉(zhuǎn)換為整型并賦給i int i=integer.parseint(str); /判斷玩家輸入值與隨機數(shù)大小,小了則給出對話框提示“小了”,大了則提示“大了”

25、if(i<rnd) javax.swing.joptionpane.showmessagedialog(null,"小了"); else if(i>rnd) javax.swing.joptionpane.showmessagedialog(null,"大了"); else /猜對后,根據(jù)猜測次數(shù)給出相應(yīng)的評價 if(counter>5) javax.swing.joptionpane.showmessagedialog(null,"恭喜你猜對了!n你猜了 "+ counter + " 次!");

26、 else joptionpane.showmessagedialog(null,"成功!n你只猜了 "+ counter + " 次!n你真是太棒了!"); system.exit(0); counter+; 5 測試分析程序運行界面如下:(1)輸入對話框:當程序運行時自動彈出的對話框,要求輸入一個數(shù)字(2)輸入不是數(shù)字時:當輸入的不是數(shù)字是會彈出錯誤輸入的對話框(3)輸入為空時:沒有輸入按了確定時,彈出不能為空的對話框(4)輸入的數(shù)比隨機數(shù)大或小時:當輸入的數(shù)比計算機產(chǎn)生的隨機數(shù)大了時彈出對話框,提示“大了”或“小了”,按確定后再重新輸入直到猜對(5

27、)輸入正確時:當輸入數(shù)字跟計算機產(chǎn)生的隨機數(shù)相同時,給出對話框,提示成功,并給出評語6 課程設(shè)計總結(jié)這次課程設(shè)計的成功來之不易,一路艱辛坎坷,由最初接到課題時的茫然不知如何動手,到后來和幫我的同學鉆研探討后一步步接近成功,有喜有猶,有了新的思路時興奮得拍手,發(fā)現(xiàn)思路行不通或發(fā)現(xiàn)編寫程序出的一個個錯誤時愁眉苦臉,真讓我痛并快樂著這種努力鉆研剔除一個個錯誤后一步步接近成功的經(jīng)驗,是我最大的收獲,讓我有了屢敗屢戰(zhàn)的優(yōu)秀品質(zhì)。通過這次課程設(shè)計我的jave編程熟練了很多,我想感謝我的指導(dǎo)老師許老師,感謝老師的細心認真的輔導(dǎo),他的幫助讓我從開始的一片茫然到后來的豁然開朗。這次課程設(shè)計能夠順利的完成,除了我個人的努力,還有同學的幫助,同時也離不開指導(dǎo)老師的答疑解惑。參考文獻1 李尊朝,蘇軍java語言程序設(shè)計(第二版)北京:中國鐵道出版社,20072 印昊java與面向?qū)ο蟪绦蛟O(shè)計教程北京:高等教育出版社,19993 耿祥義,張躍平j(luò)ava2 實用教程(第二版)北京:清華大學出版社,20064 黃同成,黃俊民,董建寅數(shù)據(jù)結(jié)構(gòu)m北京:中國電力出版社,2008附錄(部分源程序清單)import java.awt.*;import javax.swing.*;public class game public static void main

溫馨提示

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

評論

0/150

提交評論