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个蓝球
- 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 2 3 4 5 66 7 8
- //从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):");
- int num ;
- while (scan.hasNextLine())
- {
- 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;
- }
- }
运行结果:
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; //圆柱的高
- 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;
-
- /**
- * 7.2 CylinderList类
- */
- public class CylinderList
- {
- private String listName;
- private ArrayList<Cylinder> cList;
-
- CylinderList(String listName, ArrayList<Cylinder> cList)
- {
- this.listName = listName;
- this.cList = cList;
- }
-
- //返回一个代表几何名字的字符串
- public String getName()
- {
- return listName;
- }
-
- //返回代表集合中Cylinder对象的个数
- public int numberOfCylinders()
- {
- return cList.size();
- }
-
- //返回 所有的Cylinder对象的 高 的和
- public double totalHeight()
- {
- double totalHeight = 0;
- for (Cylinder cylinder : cList)
- {
- totalHeight += cylinder.getHeight();
- }
- return totalHeight;
- }
-
- //返回 所有的Cylinder对象的 圆柱底面直径 的和
- public double totalDiameter()
- {
- double totalDiameter = 0;
- for (Cylinder cylinder : cList)
- {
- totalDiameter += cylinder.diameter();
- }
- return totalDiameter;
- }
-
- //返回 所有的Cylinder对象的 面积 之和
- public double totalArea()
- {
- double totalArea = 0;
- for (Cylinder cylinder : cList)
- {
- totalArea += cylinder.area();
- }
- return totalArea;
- }
-
- //返回 所有的Cylinder对象的 体积 之和
- public double totalVolume()
- {
- double totalVolume = 0;
- for (Cylinder cylinder : cList)
- {
- totalVolume += cylinder.volumn();
- }
- return totalVolume;
- }
-
- //返回 所有的Cylinder对象 面积 的 平均值
- public double averageArea()
- {
- double averageArea = 0;
- if (cList.size()>0)
- {
- averageArea = totalArea()/cList.size();
- }
- return averageArea;
- }
-
- //返回 所有的Cylinder对象 体积 的 平均值
- public double averageVolume()
- {
- double averageVolume = 0;
- if (cList.size()>0)
- {
- averageVolume = totalVolume()/cList.size();
- }
- return averageVolume;
- }
-
- //返回 集合的名字及集合中每一个对象的toString方法
- public String toString()
- {
- String output = "\n" + listName + "\n\n";
- for (Cylinder cylinder : cList)
- {
- output += (cylinder.toString() + "\n");
- }
- return output;
- }
-
- //返回 集合的名字及Cylinder对象个数,
- //总面积,总体积,平均面积及平均体积
- public String summaryInfo()
- {
- String output = null;
- DecimalFormat df = new DecimalFormat("#,##0.0##");
- output = "-----" + listName + " Summary-----"
- + "\nNimber of Cylinders: " + numberOfCylinders()
- + "\nTotal Area: " + df.format(totalArea())
- + "\nTotal Volume: " + df.format(totalVolume())
- + "\nTotal Height: " + df.format(totalHeight())
- + "\nTotal Diameter: " + df.format(totalDiameter())
- + "\nAverage Area: " + df.format(averageArea())
- + "\nAverage Volume: " + df.format(averageVolume());
- return output;
- }
- }
7.3 CylinderListApp 测试类
- package week7;
-
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
-
- /**
- * 7.3 CylinderListApp 测试类
- * (a) 打开用户输入的文件并读取第一行作为集合的名字;之后读取其他行,
- * 依次生成Cylinder对象,最后生成CylinderList对象。
- * (b) 输出CylinderList对象(调用toString方法),之后空一行,
- * (c) 输出CylinderList对象的汇总信息(调用summaryInfo方法)
- * 注意:
- * 1)如果输入文件名后出现错误称找不到文件,此时可输出绝对路径
- * 2)读取文件第一行作为集合名称后,使用以scanFile.hasNext()
- * 为条件的while循环反复读入三行,然后创建Cylinder对象
- * 3)输出结果必须与下面测试输出的结果完全一致
- */
- public class CyliderListApp
- {
- public static void main(String[] args) throws FileNotFoundException
- {
- String lable;
- double radius;
- double height;
- Scanner scan0 = new Scanner(System.in);
- Scanner scan1 = null;
- Scanner inputStream = null;
-
- ArrayList<Cylinder> cList = new ArrayList<>(10);
- CylinderList cylinderList = null;//new CylinderList(listName, cList)
-
- System.out.print("Enter file name: ");
- String fileName = scan0.nextLine(); // cylinder_0.dat
- scan0.close();
-
- File file = new File(fileName);
- if(file.exists())
- {
- inputStream = new Scanner(file);
-
- String listName = inputStream.nextLine();
- while (inputStream.hasNextLine())
- {
- String line = inputStream.nextLine();
- //使用逗号分隔line,例:Small Example, 4.0, 10.0
- scan1 = new Scanner(line);
- scan1.useDelimiter(",");
- if (scan1.hasNext())
- {
- lable = scan1.next();
- radius = Double.parseDouble(scan1.next());
- height = Double.parseDouble(scan1.next());
- //创建Cylinder对象并加入ArrayList<Cylinder>中
- cList.add(new Cylinder(lable, radius, height));
- }
- scan1.close(); //就近原则,以免出现空指针
- }
- inputStream.close();
- //初始化CylinderList对象
- cylinderList = new CylinderList(listName, cList);
- System.out.print(cylinderList.toString());
- System.out.println(cylinderList.summaryInfo());
- }
- else
- {
- System.out.println(file.getAbsolutePath());
- }
- }
- }
-
运行结果:
8 Cylinder(三)
-------------------------2016-11-**更新
//wiating for update...
//
//run
//
9 Cylinder(四)
-------------------------2016-11-**更新
//wiating for update...
//
//run
//
时间: 2024-11-05 12:22:05