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];
- private int betBlueBall;
- 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();
-
-
- lottery.seletRedBall();
- System.out.println("--------红球选择完成-----------");
-
-
- 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---------------------------");
- }
-
-
- public void seletRedBall()
- {
-
- int[] redBall = new int[33];
- for (int i = 0; i < redBall.length; i++)
- redBall[i] = i + 1;
-
-
- boolean[] used = new boolean[redBall.length];
-
- int count = 0;
-
-
-
- scan = new Scanner(System.in);
- System.out.println("请输入6个红球号码(1-33):");
-
- while (scan.hasNext())
- {
- int num = scan.nextInt();
-
-
- if (num < 1 || num > 33)
- {
- System.out.println("没有"
- + num + "号,请选1-33号。您还需要选择"
- + (6-count) +"个红球!");
- continue;
- }
-
- if (!used[num])
- {
- betRedBall[count++] = num;
- used[num] = true;
- System.out.println("已选"
- + num + "号!您还需要选择"
- + (6-count) +"个红球!");
- }
- else
- {
- System.out.println(num + "号已选过,您还需要选择"
- + (6-count) +"个红球!");
- }
-
- if (count==6) break;
- }
- }
-
-
- public void seletBlueBall()
- {
-
-
- scan = new Scanner(System.in);
- System.out.print("请输入1个蓝球号码(1-16):");
- int num ;
- while (scan.hasNextLine())
- {
- num = scan.nextInt();
-
-
- 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 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();
-
- 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;
- }
-
-
- 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
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;
-
- System.out.print("Enter file name: ");
- String fileName = scan0.nextLine();
- 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();
-
- scan1 = new Scanner(line);
- scan1.useDelimiter(",");
- if (scan1.hasNext())
- {
- lable = scan1.next();
- radius = Double.parseDouble(scan1.next());
- height = Double.parseDouble(scan1.next());
-
- cList.add(new Cylinder(lable, radius, height));
- }
- scan1.close();
- }
- inputStream.close();
-
- 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-**更新
//run
//
9 Cylinder(四)
-------------------------2016-11-**更新
//run
//