仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1496|回复: 19
打印 上一主题 下一主题

[学习教程] JAVA编程:一个JAVA写的背单词程序

[复制链接]
小妖女 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:44:09 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
java比较简单,没有C++的烦琐,但学习时最好有C++为基础.与JSP和SQL起应用,功能强大.程序俺看了一些Java,写个程序出来玩玩。因为界面是用Jbuilder天生的,大概代码对照乱,并且还没合起来。今朝版本是0.00001/**Word.java**Createdon2004-9-26**/packagecom.henry.vocabulary;importjava.util.*;importjava.text.*;publicclassWord{privatestaticfinalSimpleDateFormatformat=newSimpleDateFormat("yyyy-M-ddhh:mm:ss");privatestaticfinalint[]memoryCurve={1,2,4,8,12,20,36,54,87,160,360};//Every"tomorrow"startsfrom5AM,notfrommiddlenight.privatestaticfinalinthourAdjust=-5;staticCalendartoday=Calendar.getInstance();{today.add(Calendar.HOUR,hourAdjust);today.set(Calendar.HOUR,12);today.set(Calendar.MINUTE,0);today.set(Calendar.SECOND,0);}privateString_wordValue;Date_nextReviewTime;int_reviewTimes;int_forgetTimes;/***@param_wordValueThe_wordValuetoset.*/publicvoidsetWordValue(String_wordValue){this._wordValue=_wordValue;}/***@returnReturnsthe_wordValue.*/publicStringgetWordValue(){return_wordValue;}publicWord(StringwordValue,StringnextReviewTime,intreviewTimes,intforgetTimes){try{setWordValue(wordValue);_nextReviewTime=format.parse(nextReviewTime);_reviewTimes=reviewTimes;_forgetTimes=forgetTimes;}catch(ParseExceptionpe){System.out.println("Theinputisnotadate!");thrownewRuntimeException(pe);}}publicWord(StringstrWord){try{String[]values=strWord.split(",");setWordValue(values[0]);_nextReviewTime=format.parse(values[1]);_reviewTimes=Integer.parseInt(values[2].trim());if(values.length==4){//forcompatibletotheoldversion_forgetTimes=Integer.parseInt(values[3]);}else{_forgetTimes=0;}}catch(ParseExceptionpe){System.out.println("Theinputisnotadate!");thrownewRuntimeException(pe);}}publicvoidforget(){_nextReviewTime=today.getTime();_reviewTimes=0;_forgetTimes++;}publicvoidremember(){CalendarnextTime=(Calendar)today.clone();nextTime.add(Calendar.DATE,memoryCurve[_reviewTimes]);_nextReviewTime=nextTime.getTime();_reviewTimes++;}publicStringtoString(){finalSimpleDateFormatformat2=newSimpleDateFormat("yyyy-M-ddhh:mm:ss");Strings=getWordValue()+","+format2.format(_nextReviewTime)+","+_reviewTimes+","+_forgetTimes;returns;}/***Judgeifthiswordneedtobereviewedtoday.*CommentforisNeedReview*/publicbooleanisNeedReview(){if(this._nextReviewTime.after(today.getTime())){returnfalse;}returntrue;}publicstaticvoidmain(String[]args){Wordw=newWord("test","2004-8-30",2,3);Wordw1=newWord("test,2004-8-30,2,3");System.out.println(w);w.remember();System.out.println(w);w.forget();System.out.println(w);System.out.println(w1);w1.remember();System.out.println(w1);w1.forget();System.out.println(w1);}}/**WordsReview.java**Createdon2004-9-26**/packagecom.henry.vocabulary;importjava.util.*;importjava.io.*;/***@authorHenry**/publicclassWordsReview{ListwordList=newArrayList();ListwordNeedReviewList=newArrayList();WordcurrentWord;/***@paramfileName*/publicvoidreadFromFile(StringfileName){try{StringBuffersb=newStringBuffer();BufferedReaderbr=newBufferedReader(newFileReader(fileName));Strings;while((s=br.readLine())!=null){this.currentWord=newWord(s);this.wordList.add(this.currentWord);if(this.currentWord.isNeedReview()){this.wordNeedReviewList.add(this.currentWord);}}this.currentWord=null;}catch(FileNotFoundExceptione){System.out.println(e);System.out.println(e.getStackTrace());}catch(IOExceptionioe){System.out.println(ioe);System.out.println(ioe.getStackTrace());}}/***@paramfileName*/publicvoidsaveToFile(StringfileName){}/***Getaword,whichneedstobereviewed.*@return*/publicStringgetWord(){if(this.wordNeedReviewList.isEmpty()){returnnull;}Randomrandom=newRandom();intramdonIndex=random.nextInt()%this.wordNeedReviewList.size();currentWord=(Word)this.wordNeedReviewList.get(ramdonIndex);returncurrentWord.getWordValue();}publicvoidsaveWord(booleanisForget){if(this.currentWord==null){System.out.println("Shouldnotcomehere!");System.out.println("WhenyoucallsaveWordtosavetheword,");System.out.println("youshouldgetthewordfirst.");return;}if(isForget){this.currentWord.forget();}else{this.currentWord.remember();this.wordNeedReviewList.remove(currentWord);}}publicstaticvoidmain(String[]args){WordsReviewwr=newWordsReview();wr.readFromFile("HenryPan.txt");Strings=wr.getWord();wr.saveWord(false);}}//MainFrame.javapackageapp2;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;/***
Title:

*
Description:

*
Copyright:Copyright(c)2004

*
Company:

*@authorHenry*@version1.0*/publicclassMainFrameextendsJFrame{JPanelcontentPane;GridBagLayoutgridBagLayout1=newGridBagLayout();JTextFieldword=newJTextField();JRadioButtonremember=newJRadioButton();JRadioButtonforget=newJRadioButton();JButtonnext=newJButton();ButtonGroupbuttonGroup1=newButtonGroup();//ConstructtheframepublicMainFrame(){enableEvents(AWTEvent.WINDOW_EVENT_MASK);try{jbInit();}catch(Exceptione){e.printStackTrace();}}//ComponentinitializationprivatevoidjbInit()throwsException{contentPane=(JPanel)this.getContentPane();word.setFont(newjava.awt.Font("Dialog",0,16));word.setSelectionStart(0);word.setText("");contentPane.setLayout(gridBagLayout1);this.setSize(newDimension(560,463));this.setTitle("TAFUVocabulary");remember.setFont(newjava.awt.Font("Dialog",0,16));remember.setText("Irememberitwell.");forget.setFont(newjava.awt.Font("Dialog",0,16));forget.setSelected(true);forget.setText("Iforgetit.");next.setFont(newjava.awt.Font("Dialog",0,16));next.setText("Next");next.addActionListener(newFrame1_next_actionAdapter(this));contentPane.add(word,newGridBagConstraints(0,0,2,1,0.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.NONE,newInsets(17,9,0,31),494,39));contentPane.add(remember,newGridBagConstraints(0,1,1,1,0.0,0.0,GridBagConstraints.SOUTHWEST,GridBagConstraints.NONE,newInsets(26,0,1,0),0,0));contentPane.add(forget,newGridBagConstraints(1,1,1,2,0.0,0.0,GridBagConstraints.SOUTHWEST,GridBagConstraints.NONE,newInsets(25,0,37,0),0,0));contentPane.add(next,newGridBagConstraints(1,3,1,1,0.0,0.0,GridBagConstraints.CENTER,GridBagConstraints.NONE,newInsets(0,9,59,13),44,11));buttonGroup1.add(forget);buttonGroup1.add(remember);}//OverriddensowecanexitwhenwindowisclosedprotectedvoidprocessWindowEvent(WindowEvente){super.processWindowEvent(e);if(e.getID()==WindowEvent.WINDOW_CLOSING){System.exit(0);}}voidnext_actionPerformed(ActionEvente){System.out.println("Nextactionperform!");if(this.forget.isSelected()){}}}classFrame1_next_actionAdapterimplementsjava.awt.event.ActionListener{MainFrameadaptee;Frame1_next_actionAdapter(MainFrameadaptee){this.adaptee=adaptee;}publicvoidactionPerformed(ActionEvente){adaptee.next_actionPerformed(e);}}//MainWin.javapackageapp2;importjavax.swing.UIManager;importjava.awt.*;/***
Title:

*
Description:

*
Copyright:Copyright(c)2004

*
Company:

*@authornotattributable*@version1.0*/publicclassMainWin{booleanpackFrame=false;//ConstructtheapplicationpublicMainWin(){MainFrameframe=newMainFrame();//Validateframesthathavepresetsizes//Packframesthathaveusefulpreferredsizeinfo,e.g.fromtheirlayoutif(packFrame){frame.pack();}else{frame.validate();}//CenterthewindowDimensionscreenSize=Toolkit.getDefaultToolkit().getScreenSize();DimensionframeSize=frame.getSize();if(frameSize.height>screenSize.height){frameSize.height=screenSize.height;}if(frameSize.width>screenSize.width){frameSize.width=screenSize.width;}frame.setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);frame.setVisible(true);}//Mainmethodpublicstaticvoidmain(String[]args){try{UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}catch(Exceptione){e.printStackTrace();}newMainWin();}}

C++编译的是本地码,优点是启动快,而且可以精确控制资源因此可以开发很高效的程序.缺点是编程麻烦,而且容易留下安全隐患.跨平台靠源代码在各个平台间分别编译(一处编写到处编译)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-10 14:10

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表