收藏 分销(赏)

程序设计综合训练报告.doc

上传人:仙人****88 文档编号:9345394 上传时间:2025-03-23 格式:DOC 页数:15 大小:221KB 下载积分:10 金币
下载 相关 举报
程序设计综合训练报告.doc_第1页
第1页 / 共15页
程序设计综合训练报告.doc_第2页
第2页 / 共15页


点击查看更多>>
资源描述
程序设计综合训练报告 《程序设计综合训练》 设 计 报 告 题 目: 猜数字游戏 专 业: 软件工程 班 级: 学 号: 姓  名: 指导教师:     成  绩: 2012年2月 一、引言 1.1 项目的名称 猜数字游戏 1.2 项目目标 通过本课程设计使学生能够较全面的掌握面向对象程序设计的有关概念和开发方法,以便能较全面地理解、掌握和综合运用所学的知识,提高自身的编程能力。 1.3 顶目内容及要求 设计一个猜数字游戏程序,用户随机输入数字,当与计算机产生的随机数相符合时,计算机将提醒用户,游戏成功的信息,当有偏差时,将会提醒偏差,如偏大或者是偏小。 二、需求分析 2.1 系统概述 设计一个猜数字游戏程序,要求具体如下特性: 1.输入任意一个数字。数字的要求是1—100的自然数。 2.系统对您输入的数字进行判断。 3. 游戏开始时,系统自动记录您猜数字的时间,结束后显示游戏共花费的时间。 4. 每猜测一次,系统会记录下来,游戏结束后,显示共猜测多少次。 5. 在游戏结束时,可以选择关闭游戏,或者再来一局。 2.2 系统运行环境 Windows XP eclipse 中文版 三、总体设计 3.1 系统总体设计 猜数字游戏,随机给出一个[1,100]之间的数字,然后让你猜是什么数字。你可以随便猜一个数字,游戏会提示太大还是太小。经过几次猜测与提示后,最终推出答案。 游戏开始后在字符界面。用户输入所猜想的数字,按“确定”按钮表示确定,按照提示语句操作,直至游戏结束。同时,计算机开始对游戏进行计时和计数功能。 产生随机数 3.2 模块设计 记录猜测的次数 开始游戏 记录时间 游戏界面 四、详细设计 4.1 主界面模块 随机数的产生模块 Random random = new Random(); number = random.nextInt(100); // 产生一个1-100间的随机数 输入判断模块 jTextField1.setText("");// 清空文本框内容 jTextField1.requestFocus();// 文本框获取输入焦点 输入输出模块 if (guess > number) { JOptionPane.showMessageDialog(null, "猜大了哦!!"); jTextField1.setText(""); jTextField1.requestFocus(); 成绩判断模块 // 判断成绩。 switch (counter) { case 1: JOptionPane.showMessageDialog(null, "你是神么?? 这也太给力了吧!!"); break; case 2: case 3: case 4: case 5: case 6: case 7: JOptionPane.showMessageDialog(null, "这么快就猜对了,很不错哦!"); break; default: JOptionPane.showMessageDialog(null, "猜了半天才猜出来,还要多多练习哦!"); break; } JOptionPane.showMessageDialog(null, "您总共猜了" + counter + "次 共花了" + (endTime - startTime) / 1000 + "秒的时间答对正确答案!"); } jTextField1.setText(""); jTextField1.requestFocus(); } 五、实现和源程序 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.Random; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.WindowConstants; import java.awt.FlowLayout; public class GuessNum extends javax.swing.JFrame { private JLabel jLabel1; private JTextField jTextField1; private JButton jButton1; private int number = 0; private int counter = 0; long startTime = System.currentTimeMillis(); long endTime; public GuessNum() { super ("猜数字"); initChuankou(); Random random = new Random(); number = random.nextInt(100); } private void initChuankou() { try { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); getContentPane().setLayout(null); JFrame frame = new JFrame("Test Buttons"); frame.setLayout(new FlowLayout()); jLabel1 = new JLabel(); getContentPane().add(jLabel1); jLabel1.setText("<html>欢迎进入猜数字游戏!请您输入1~100中的任意一个数:</html>"); jLabel1.setBounds(2, 0, 200, 50); jTextField1 = new JTextField(); getContentPane().add(jTextField1); jTextField1.setBounds(50, 60, 112, 28); jTextField1.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { jTextField1KeyPressed(evt); } }); jButton1 = new JButton(); getContentPane().add(jButton1); jButton1.setText("确定"); jButton1.setBounds(70, 110, 60, 28); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { jButton1ActionPerformed(evt); } }); pack(); this.setSize(220, 200); setLocationRelativeTo(null); } catch (Exception e) { e.printStackTrace(); } setVisible(true); } private void jButton1ActionPerformed(ActionEvent evt) { int guess = 0; counter++; try { guess = Integer.parseInt(jTextField1.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(null, "对不起,您输入的数字不合法,请输入一个1-100之间的整数!"); jTextField1.setText(""); jTextField1.requestFocus(); return; } if (guess > number) { JOptionPane.showMessageDialog(null, "猜大了哦!!"); jTextField1.setText(""); jTextField1.requestFocus(); return; } if (guess < number) { JOptionPane.showMessageDialog(null, "猜小了哦!!"); jTextField1.setText(""); jTextField1.requestFocus(); return; } if (guess == number) { endTime = System.currentTimeMillis(); switch (counter) { case 1: JOptionPane.showMessageDialog(null, "你好给力哦!!太厉害了!"); break; case 2: case 3: case 4: case 5: case 6: case 7: JOptionPane.showMessageDialog(null, "很不错哦!很快就猜对了呢!!"); break; default: JOptionPane.showMessageDialog(null, "现在才猜出来,需要多加练习哦!"); break; } JOptionPane.showMessageDialog(null, "您总共猜了" + counter + "次 共花了" + (endTime - startTime) / 1000 + "秒的时间答对正确答案!"); } jTextField1.setText(""); jTextField1.requestFocus(); } private void jTextField1KeyPressed(KeyEvent evt) { if (evt.getKeyCode() == KeyEvent.VK_ENTER) { jButton1.doClick(); } } public static void main(String[] args) { System.out.println("欢迎您来到猜数字游戏"); new GuessNum(); } } 六、程序设计综合训练心得与体会 经过这几天努力终于完成了这份课,虽然在过程中我都遇到了许多的困难,但是却使我得到了许多的收获。在写代码的过程中遇到了一些的困难,就是对一些有关Java语言的不理解,我便又到课本中去查找。以前上课时有许多的问题并没有真正的认识到,但通过这次试验的制作,使我掌握了许多更重要的知识点。 七、参考文献 谭诰强.《Java程序设计》 清华大学出版社 2006年8月第2版 java上机训练(一) 第二题:   import java.io.*;   public class JModify1{   public static void main(String args[]){   CVowel v=new CVowel();   v.printCount();   }   }   class InputData{   static private String s="";   /****1****/   public void input(){ public static void input(){   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));   try{   s=br.readLine();   }catch(IOException e){}   }   static public String getString(){   input();   return s;   }   }   class CVowel{   private String VowelChar="aeiouAEIOU";   private int count=0;   private String s="";   void printCount(){   System.out.println("请输入一个字符串:");   s=InputData.getString();   char vowel[]=s.toCharArray();   for(int i=0;i<vowel.length;i++)   /*****2*****/   if(VowelChar.indexOf(vowel[i])==true) if(VowelChar.indexOf(vowel[i])!=-1)   count++;   System.out.println("元音字母个数为:"+count);   }   } 来源:考试大-计算机二级考试 第二题: import java.io.*; public class JDesign1 { public static void main(String args[]) throws IOException{ BufferedWriter out=new BufferedWriter(new FileWriter("file.txt")); System.out.println(get()); out.write(String.valueOf(get())); out.flush(); } static int get(){ int n=0; for(int i=1000;i<10000;i++) if(i%10==6&&i%3==0) n++; return n; } } 第三题:  public static void f(int n){   int i=2;   while(n>1){   ____if(n%i==0){___   System.out.println(i);   n/=i;   }   else   ___i++;___   }   }   public static void main(String args[]){   int n=100;   f(n);   }   } 来源:考试大-计算机二级考试 第四题: import java.io.*; public class JDesign2 { public static void main(String args[]) throws IOException{ BufferedWriter out=new BufferedWriter(new FileWriter("file.txt")); System.out.println(get()); out.write(String.valueOf(get())); out.flush(); } static int get(){ int i,j,k,n=100,m=0; for(n=100;n<1000;n++) { i=n%10; j=n%100/10; k=n/100; if(i*i*i+j*j*j+k*k*k==n) m++; } return m; } } Java上机训练(二) 第一题: import javax.swing.JOptionPane; public class java_1 { public static void main(String args[] ){ int x, y, result; String xval, yval; xval = JOptionPane.showInputDialog( "输入第1个整数:" ); yval = JOptionPane.showInputDialog( "输入第2个整数:" ); //*********found******** x = Integer.parseInt( xval ); y = Integer.parseInt( yval ); result = x * y; //*********found******** JOptionPane.showInputDialog( null, "两个数的积: " + result ); System.exit(0); } }   第二题: import java.io.*; public class java_2{ //*********found******** public static void main(String args[]) throws Exception{ long filepoint = 0 ; String s; RandomAccessFil0e file = new RandomAccessFile("C:\\eclipse\\work_home\\huang\\src\\java_2.java","r"); long filelength = file.length(); while (filepoint<filelength){ //*********found******** s =file.readLine(); System.out.println(s); filepoint = file.getFilePointer(); } file.close(); } } 第三题: import javax.swing.JOptionPane; public class java_3 { public static void main(String args[]) { int a,b,c,d,x,t; String i; i = JOptionPane.showInputDialog( "请输入一个四位整数:" ); x= Integer.parseInt(i); a=(x%10+5)%10; b=(x/10%10+5)%10; c=(x/100%10+5)%10; d=(x/1000+5)%10; t=a; a=d; d=t; t=b; b=c; c=t; x=a+b*10+c*100+d*1000; System.out.println(x); } } java上机训练(三) 第一题: 1、 import java.util.*; public class Vehicle { public void main(String args[]){ Vehicle v=new Vehicle(); v.run(); } String run(){ System.out.println("这是交通工具run方法"); } } 2、class Motor extends Vehicle { public String getRun() { return run; } public String setRun1() { return run1; } private String run; private String run1="这个交通工具是汽车"; } class Ship extends Vehicle { public String getRun() { return run; } public String getRun2() { return run2; } private String run; private String run2="这个交通工具是船"; } class Aeroplane extends Vehicle { public String getRun() { return run; } public String setRun3() { return run3; } private String run; private String run3="这个交通工具是飞机"; } 3、class Bus extends Motor { public String getRun() { return run; } public String getRun4() { return run4; } private String run; private String run4="这个交通工具是Bus"; } class Car extends Motor { public String getRun() { return run; } public String getRun5() { return run5; } private String run; private String run5="这个交通工具是Car"; } 4、public class Test { public static void main(String[] args) { Vehicle a=new Vehicle(); Motor b=new Motor(); Ship c=new Ship(); Aeroplane d=new Aeroplane(); Bus e=new Bus(); Car f=new Car(); a.run1(); b.run2(); c.run3(); d.run4(); e.run5(); System.out.println("这是交通工具的run方法"); System.out.println("这是汽车的run方法"); System.out.println("这是船的run方法"); System.out.println("这是飞机的run方法"); System.out.println("这是Bus的run方法"); System.out.println("这是Car的run方法"); } } 第二题: import java.io.BufferedReader; import java.io.InputStreamReader; public class kaifang extends Exception{ public static void main(String[] args){ kaifang n=new kaifang(); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try {String s= br.readLine(); n.test(Double.parseDouble(s)); } catch(Exception d){ System.out.println("error"); } } void test(double d) { if(d<0.0) System.out.println("error"); else System.out.println(Math.sqrt(d)); } } java上机训练(四) 第一题 public class A { int a; int b; public A(int a, int b) { super(); this.a=a; this.b=b; } public A() { super(); } } public class B extends A { int c; public B(int c){ super(); this.c=c; } public B (int a,int b,int c){ super(a,b); this.c=c; } } public class Test { public static void main (String [] args ) { B b1=new B(5); A a2=new A(3,4); int add=b1.c+a2.a+a2.b; System.out.println("a+b+c="+add); } } 第二题 import java.io.*; public class PaiXu { public static void main(String args[]) throws Exception{ int a[]=new int[10]; int i,j,t; i=j=t=0; BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入10个整数:"); for(;i<10;i++){ a[i]=Integer.parseInt(reader.readLine()); } for(i=0;i<9;i++){ for(j=i+1;j<10;j++){ if(a[i]>a[j]){ t=a[i]; a[i]=a[j]; a[j]=t; }} } System.out.println("递增排序后的10个数:"); for(i=0;i<10;i++)System.out.print(a[i]+" "); }} 第三题 import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; public class A3 implements ActionListener{ JFrame frame=new JFrame("求两个数的和"); JButton button=new JButton("求和"); JLabel label1=new JLabel("输入第一个数:"); JLabel label2=new JLabel("输入第二个数:"); JTextField text1=new JTextField(15); JTextField text2=new JTextField(15); JTextField text3=new JTextField(15); public static void main(String[] args) { A3 frameDemo=new A3(); frameDemo.go(); } void go(){ Container contentPane=frame.getContentPane(); contentPane.setLayout(new GridLayout(3,2,10,10)); contentPane.add(label1); contentPane.add(text1); contentPane.add(label2); contentPane.add(text2); contentPane.add(button); contentPane.add(text3); button.addActionListener(this); frame.pack(); frame.setBounds(0, 0, 300, 150); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { int a=Integer.parseInt(text1.getText()); int b=Integer.parseInt(text2.getText()); int c=a+b; text3.setText(String.valueOf(c)); } } 第四题 import java.io.BufferedReader; import java.io.InputStreamReader; public class Nan { public static void main(String args[]) throws Exception{ int a[]=new int[10]; int i; i=0; BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入10个整数:"); for(;i<10;i++){ a[i]=Integer.parseInt(reader.readLine()); } for(i=9;i>=0;i--) System.out.print(a[i]+" "); } } 第五题 import java.util.Scanner; public class Come { public static void main( String[ ] args ){ System.out.print( "What's your name ? " ); Scanner sc = new Scanner(System.in); System.out.print(sc.next( )+"Wecome You ! " ); } } 15
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服