java-7311练习(下)
Posted bl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java-7311练习(下)相关的知识,希望对你有一定的参考价值。
java练习,仅供参考!
欢迎同学们交流讨论。
JDK 1.8 API帮助文档
JDK 1.6 API中文文档
第一次小组作业:模拟双色球彩票
第一次小组作业(一) 控制台版

游戏规则:
• 双色球为红球和蓝球;
• 用户从1-33中自选6个数字(不能重复)代表红球;从1-16中自选1个数字代表蓝球;
• 上图为中奖规则,如一等奖为中6个红球及蓝球,二等奖为仅中6个红球……
• 请自拟六个奖项对应的奖品。

package GroupFirst; import java.util.Scanner; /** * 第一次小组作业:模拟双色球彩票 * •游戏规则 * •双色球为红球和蓝球 * •用户从1-33中自选6个数字(不重复)代表红球;从1-16中自选1个数字代表蓝球 * •上图为中奖规则,如一等奖为中6个红球及蓝球,二等奖为仅中6个红球…… * •请自拟六个奖项对应的奖品 */ public class Balllottery { private int[] betRedBall = new int[6];//存放选择的6个红球 private int betBlueBall; //存放选择的1个蓝球 private Scanner scan = null; //扫描器对象 private int[] winningRedBall = {1,2,3,4,5,6};//红球中奖号码 private int winningBlueBall = 7; //蓝球中奖号码 public static void main(String[] args) { Balllottery lottery = new Balllottery(); //从1-33中自选6个数字(不重复)代表红球 lottery.seletRedBall();//lottery.seletRedBall(); System.out.println("--------红球选择完成-----------"); //从1-16中自选1个数字代表蓝球 lottery.seletBlueBall(); System.out.println("--------蓝球选择完成-----------"); //投注并开奖; int level = lottery.lotteryBetting(); //显示奖品; lottery.showResults(level); } public void showResults(int level) { System.out.println("---------------------------"); System.out.print("您的投注为:"); for (int i = 0; i < betRedBall.length; i++) System.out.printf("%-3d",betRedBall[i]); System.out.print(", " + betBlueBall + "\\n"); System.out.print("开奖号码为:"); for (int i = 0; i < winningRedBall.length; i++) System.out.printf("%-3d",winningRedBall[i]); System.out.print(", " + winningBlueBall + "\\n\\n"); //根据中奖等级分配奖品 switch (level) { case 0: System.out.println("抱歉,您没中奖!"); break; case 1: System.out.println("一等奖,恭喜您获得自行车一辆!"); break; case 2: System.out.println("二等奖,恭喜您获得保温杯一个!"); break; case 3: System.out.println("三等奖,恭喜您获得新书包一个!"); break; case 4: System.out.println("四等奖,恭喜您获得记事本一个!"); break; case 5: System.out.println("五等奖,恭喜您获得签字笔一个!"); break; case 6: System.out.println("六等奖,恭喜您获得黑铅笔一个!"); break; } System.out.println("\\n---------------------------"); } // 从1-33中自选6个数字(不重复)代表红球 public void seletRedBall() { //用一个数组来存放33个红球并赋值1-33号 int[] redBall = new int[33]; for (int i = 0; i < redBall.length; i++) redBall[i] = i + 1; // 1--33 // used表示已经出现过的红球 ; boolean数组默认初始为false boolean[] used = new boolean[redBall.length]; int count = 0; //统计下注红球个数 // 输入6个不重复的红球号码,并存放到bet数组 //Scanner scan = null; scan = new Scanner(System.in); System.out.println("请输入6个红球号码(1-33):"); while (scan.hasNext()) { int num = scan.nextInt(); // 获得输入 // 如果这个号码是1-33号,那么就重新选择 if (num < 1 || num > 33) { System.out.println("没有" + num + "号,请选1-33号。您还需要选择" + (6-count) +"个红球!"); continue; } // 如果这个号码没有被选过,那么就放到bet数组 if (!used[num]) { betRedBall[count++] = num; used[num] = true; System.out.println("已选" + num + "号!您还需要选择" + (6-count) +"个红球!"); } else { System.out.println(num + "号已选过,您还需要选择" + (6-count) +"个红球!"); } // 选完6个红球则跳出循环 if (count==6) break; } } // 从1-16中自选1个数字代表蓝球 public void seletBlueBall() { // 输入1个蓝球号码 //Scanner scan = null; scan = new Scanner(System.in); System.out.print("请输入1个蓝球号码(1-16):"); while (scan.hasNextLine()) { int num = scan.nextInt(); // 获得输入 // // 如果这个号码是1-16号,那么就重新选择 if (num < 1 || num > 16) { System.out.println("没有" + num + "号,请选1-16号!"); continue; } else { betBlueBall = num; System.out.println("已选" + num + "号!"); break; } } } // 投注并开奖 public int lotteryBetting() { int correctRedCount = 0; // 猜中的红球个数 boolean correctBlueCount = false; // 是否猜中篮球 //遍历选择的红球;对比开奖结果 算出中奖的红球个数 for (int i = 0; i < betRedBall.length; i++) { for (int j = 0; j < winningRedBall.length; j++) { if (betRedBall[i] == winningRedBall[j]) { correctRedCount++; continue; } } } // 判断是否猜中蓝球 if (betBlueBall == winningBlueBall) correctBlueCount = true; /** 没中奖 返回 0 * 一等奖 中 6+1 * 二等奖 中 6+0 * 三等奖 中 5+1 * 四等奖 中 5+0 中 4+1 * 五等奖 中 4+0 中 3+1 * 六等奖 中 2+1 中 0+1 中 1+1 */ System.out.println("Debug:" + correctRedCount + "," + correctBlueCount); if (correctRedCount == 6) { if (correctBlueCount) return 1; else return 2; } if (correctRedCount == 5) { if (correctBlueCount) return 3; else return 4; } if (correctRedCount == 4) { if (correctBlueCount) return 4; else return 5; } if (correctBlueCount) { if (correctRedCount == 3) return 5; //else if (correctRedCount == 2) return 6; //else if (correctRedCount == 1) return 6; else return 6; } return 0; } }
运行结果:
第一次小组作业(二) 界面版
-------------------------2016-11-23 更新
整体概况:
-
JPanel面板的的建立与更新
窗口所有的组件都是绘制在JPanel上的,主要的变化来自于球的变化;(鼠标单击事件)获取的坐标定位到具体的球,才方便操作球的变化;每一次的变化都要更新面板内容。 -
开奖号码的绑定与设置
开奖号码显示在一个JLabel标签中,这样JFrame窗体直接通过JLabel就获取开奖号码;开奖号码按钮可以设置开奖号码,而不(方便)用通过ControlBall控制球类获取。 -
单击球的变化与清空选择
我是定义了(红蓝灰)3种类型的球,根据她们的状态来响应不同的颜色或号码;清空选择按钮是分别执行了模拟单个点击已选球的操作。 -
优化与改进
(1) 所有的绘制在JPanel上, 故绘制的坐标也是基于JPanel;而JPanel面板又被添加到JFrame窗体上,又因鼠标坐标却是基于JFrame窗体的;这两套坐标不一致,但却要用鼠标的坐标去定位球的坐标,球的坐标相对固定,而JPanel的零点相对JFrame窗体的零点却可能不一致,比如窗体边框发生变化时;两套坐标的对应是个问题,我这里只是用到当前状态的相对差距(9,30),如果窗体发生变化,可能就会不能对应坐标,这样也就会产生 选球“失灵”的情况。
(2) 设置开奖号码我这里只做了范围判断,并没有做重复判断,算是个小Bug。
(3) 这里用的的是鼠标响应时间,加上代码可能效率不高,这样不可避免的有延时;其实这里的所有的球可以换作按钮,这样通过组件操作必然方便准确,这样只需重点考虑按钮美观的问题。
运行演示: 我这里已经打包可直接运行(需要jdk环境), 点击下载 .Jar 。

目录结构: 共享的源码只有5个类,有兴趣调试的可以参考此目录。

源码共享: 代码比较长,不在这里贴码了,有兴趣的同学可以点击下载 .rar。
week7 Cylinder(二)
2个文件 cylinder_1.dat, cylinder_0.dat都放在项目根目录的resource包下,内容如下:

7.1 Cylider.java
- package week7;
-
- import java.text.DecimalFormat;
-
- /**
- * 7.1 创建 Cylinder类,以存储标签、半度;
- * 方法包括获得及设置这些成员变量,计算直径、周长面积及体积。
- */
- public class Cylinder
- {
- private String lable; //存储标签
- private double radius; //圆柱半径
- private double height; //圆柱的高
- public Cylinder(String lable, double radius, double height)
- {
- this.lable = lable;
- this.radius = radius;
- this.height = height;
- }
-
- public String getLable()
- {
- return lable;
- }
-
- public boolean setLable(String lable)
- {
- boolean flag = true;
-
- if (lable.isEmpty()) flag = false;
- else this.lable = lable.trim();
- //String.trim()截去字符串开头和末尾的空白
- return flag;
- }
-
- public double getRadius()
- {
- return radius;
- }
-
- public void setRadius(double radius)
- {
- this.radius = radius;
- }
-
- public double getHeight()
- {
- return height;
- }
-
- public void setHeight(double height)
- {
- this.height = height;
- }
-
- //返回圆柱底面直径
- public double diameter()
- {
- return radius * 2;
- }
-
- //返回圆柱底面周长
- public double circumference()
- {
- return diameter() * Math.PI;
- }
-
- //返回 表面积 = 圆柱底面积×2 + 底面周长×高
- public double area()
- {
- return Math.PI * radius * radius * 2
- + circumference() * height;
- }
-
- //返回 圆柱底体积
- public double volumn()
- {
- return Math.PI * radius * radius * height;
- }
-
- @Override
- public String toString()
- {
- String output = null;
- DecimalFormat df = new DecimalFormat("#,##0.0##");
- output = lable
- + " is a cylinder with radius = " + df.format(radius)
- + " units and height = " + df.format(height)
- + " units, "
- + "\\nwhich has diameter = " + df.format(diameter())
- + " units, circumference = " + df.format(circumference())
- + " units, "
- + "\\narea = " + df.format(area())
- + " square units, and volume = " + df.format(volumn())
- + " cubic units.\\n";
- return output;
- }
-
- public static void main(String[] args)
- {
- Cylinder c1 = new Cylinder("Small Example", 4.0, 10.0);
- Cylinder c2 = new Cylinder("Medium Example", 22.1, 30.6);
- Cylinder c3 = new Cylinder("Large Example", 100.0, 200.0);
- c1.setLable("");
- System.out.println(c1);
- System.out.println(c2);
- System.out.println(c3);
- }
- }
-
7.2 CylinderList.java
- package week7;
-
- import java.text.DecimalFormat;
- import java.util.ArrayList;