首页 > 编程语言 > 详细

java-7311练习(下)

时间:2016-11-18 23:08:12      阅读:338      评论:0      收藏:0      [点我收藏+]

java练习,仅供参考!
欢迎同学们交流讨论。

JDK 1.8 API帮助文档
JDK 1.6 API中文文档

第一次小组作业:模拟双色球彩票

技术分享

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

  1. package GroupFirst; 
  2.  
  3. import java.util.Scanner; 
  4.  
  5. /** 
  6. * 第一次小组作业:模拟双色球彩票  
  7. * ?游戏规则 
  8. * ?双色球为红球和蓝球 
  9. * ?用户从1-33中自选6个数字(不重复)代表红球;从1-16中自选1个数字代表蓝球 
  10. * ?上图为中奖规则,如一等奖为中6个红球及蓝球,二等奖为仅中6个红球…… 
  11. * ?请自拟六个奖项对应的奖品 
  12. */ 
  13. public class Balllottery 
  14. { 
  15. private int[] betRedBall = new int[6]; //存放选择的6个红球 
  16. private int betBlueBall; //1个蓝球 
  17. Scanner scan = null; //扫描器对象 
  18. private int[] winningRedBall = {1,2,3,4,5,6}; //红球中奖号码 
  19. private int winningBlueBall = 7; //蓝球中奖号码 
  20.  
  21. public static void main(String[] args) 
  22. { 
  23. Balllottery lottery = new Balllottery(); 
  24. //1 2 3 4 5 66 7 8 
  25. //从1-33中自选6个数字(不重复)代表红球 
  26. lottery.seletRedBall();//lottery.seletRedBall(); 
  27. System.out.println("--------红球选择完成-----------"); 
  28.  
  29. //从1-16中自选1个数字代表蓝球 
  30. lottery.seletBlueBall(); 
  31. System.out.println("--------蓝球选择完成-----------"); 
  32.  
  33. //投注并开奖; 
  34. int level = lottery.lotteryBetting(); 
  35. //显示奖品;  
  36. lottery.showResults(level); 
  37.  
  38. } 
  39.  
  40. public void showResults(int level) 
  41. { 
  42. System.out.println("---------------------------"); 
  43. System.out.print("您的投注为:"); 
  44. for (int i = 0; i < betRedBall.length; i++) 
  45. System.out.printf("%-3d",betRedBall[i]); 
  46. System.out.print(", " + betBlueBall + "\n"); 
  47.  
  48. System.out.print("开奖号码为:"); 
  49. for (int i = 0; i < winningRedBall.length; i++) 
  50. System.out.printf("%-3d",winningRedBall[i]); 
  51. System.out.print(", " + winningBlueBall + "\n\n"); 
  52.  
  53. //根据中奖等级分配奖品 
  54. switch (level) 
  55. { 
  56. case 0: System.out.println("抱歉,您没中奖!"); break; 
  57. case 1: System.out.println("一等奖,恭喜您获得自行车一辆!"); break; 
  58. case 2: System.out.println("二等奖,恭喜您获得保温杯一个!"); break; 
  59. case 3: System.out.println("三等奖,恭喜您获得新书包一个!"); break; 
  60. case 4: System.out.println("四等奖,恭喜您获得记事本一个!"); break; 
  61. case 5: System.out.println("五等奖,恭喜您获得签字笔一个!"); break; 
  62. case 6: System.out.println("六等奖,恭喜您获得黑铅笔一个!"); break; 
  63. } 
  64. System.out.println("\n---------------------------"); 
  65. } 
  66.  
  67. // 从1-33中自选6个数字(不重复)代表红球 
  68. public void seletRedBall() 
  69. { 
  70. //用一个数组来存放33个红球并赋值1-33号 
  71. int[] redBall = new int[33];  
  72. for (int i = 0; i < redBall.length; i++) 
  73. redBall[i] = i + 1; // 1--33 
  74.  
  75. // used表示已经出现过的红球 ; boolean数组默认初始为false 
  76. boolean[] used = new boolean[redBall.length]; 
  77.  
  78. int count = 0; //统计下注红球个数 
  79.  
  80. // 输入6个不重复的红球号码,并存放到bet数组 
  81. //Scanner scan = null; 
  82. scan = new Scanner(System.in); 
  83. System.out.println("请输入6个红球号码(1-33):"); 
  84.  
  85. while (scan.hasNext()) 
  86. { 
  87. int num = scan.nextInt(); // 获得输入 
  88.  
  89. // 如果这个号码是1-33号,那么就重新选择 
  90. if (num < 1 || num > 33) 
  91. { 
  92. System.out.println("没有" 
  93. + num + "号,请选1-33号。您还需要选择" 
  94. + (6-count) +"个红球!"); 
  95. continue; 
  96. } 
  97. // 如果这个号码没有被选过,那么就放到bet数组 
  98. if (!used[num]) 
  99. { 
  100. betRedBall[count++] = num; 
  101. used[num] = true; 
  102. System.out.println("已选" 
  103. + num + "号!您还需要选择" 
  104. + (6-count) +"个红球!"); 
  105. } 
  106. else  
  107. { 
  108. System.out.println(num + "号已选过,您还需要选择" 
  109. + (6-count) +"个红球!"); 
  110. } 
  111. // 选完6个红球则跳出循环 
  112. if (count==6) break; 
  113. } 
  114. } 
  115.  
  116. // 从1-16中自选1个数字代表蓝球  
  117. public void seletBlueBall() 
  118. { 
  119. // 输入1个蓝球号码 
  120. //Scanner scan = null; 
  121. scan = new Scanner(System.in); 
  122. System.out.print("请输入1个蓝球号码(1-16):"); 
  123. int num ; 
  124. while (scan.hasNextLine()) 
  125. { 
  126. num = scan.nextInt(); // 获得输入 
  127. // 
  128. // 如果这个号码是1-16号,那么就重新选择 
  129. if (num < 1 || num > 16) 
  130. { 
  131. System.out.println("没有" 
  132. + num + "号,请选1-16号!"); 
  133. continue; 
  134. } 
  135. else 
  136. { 
  137. betBlueBall = num; 
  138. System.out.println("已选" 
  139. + num + "号!"); 
  140. break; 
  141. } 
  142. } 
  143. } 
  144.  
  145. // 投注并开奖 
  146. public int lotteryBetting() 
  147. { 
  148. int correctRedCount = 0; // 猜中的红球个数 
  149. boolean correctBlueCount = false; // 是否猜中篮球 
  150.  
  151. //遍历选择的红球;对比开奖结果 算出中奖的红球个数 
  152. for (int i = 0; i < betRedBall.length; i++) 
  153. { 
  154. for (int j = 0; j < winningRedBall.length; j++) 
  155. { 
  156. if (betRedBall[i] == winningRedBall[j]) 
  157. { 
  158. correctRedCount++; 
  159. continue; 
  160. } 
  161. } 
  162. } 
  163.  
  164. // 判断是否猜中蓝球 
  165. if (betBlueBall == winningBlueBall) correctBlueCount = true;  
  166.  
  167. /** 没中奖 返回 0 
  168. * 一等奖 中 6+1 
  169. * 二等奖 中 6+0 
  170. * 三等奖 中 5+1 
  171. * 四等奖 中 5+0 中 4+1 
  172. * 五等奖 中 4+0 中 3+1 
  173. * 六等奖 中 2+1 中 0+1 中 1+1 
  174. */ 
  175. System.out.println("Debug:" 
  176. + correctRedCount + "," + correctBlueCount); 
  177. if (correctRedCount == 6) 
  178. { 
  179. if (correctBlueCount) return 1; 
  180. else return 2; 
  181. } 
  182. if (correctRedCount == 5) 
  183. { 
  184. if (correctBlueCount) return 3; 
  185. else return 4; 
  186. } 
  187. if (correctRedCount == 4) 
  188. { 
  189. if (correctBlueCount) return 4; 
  190. else return 5; 
  191. } 
  192. if (correctBlueCount) 
  193. { 
  194. if (correctRedCount == 3) return 5; 
  195. //else if (correctRedCount == 2) return 6; 
  196. //else if (correctRedCount == 1) return 6; 
  197. else return 6; 
  198.  
  199. } 
  200. return 0; 
  201. } 
  202. } 

运行结果:

技术分享

week7 Cylinder(二)

2个文件 cylinder_1.dat, cylinder_0.dat都放在项目根目录的resource包下,内容如下:

技术分享

7.1 Cylider.java

  1. package week7; 
  2.  
  3. import java.text.DecimalFormat; 
  4.  
  5. /** 
  6. * 7.1 创建 Cylinder类,以存储标签、半度; 
  7. * 方法包括获得及设置这些成员变量,计算直径、周长面积及体积。  
  8. */ 
  9. public class Cylinder 
  10. { 
  11. private String lable; //存储标签 
  12. private double radius; //圆柱半径 
  13. private double height; //圆柱的高 
  14. Cylinder(String lable, double radius, double height) 
  15. { 
  16. this.lable = lable; 
  17. this.radius = radius; 
  18. this.height = height; 
  19. } 
  20.  
  21. public String getLable() 
  22. { 
  23. return lable; 
  24. } 
  25.  
  26. public boolean setLable(String lable) 
  27. { 
  28. boolean flag = true; 
  29.  
  30. if (lable.isEmpty()) flag = false; 
  31. else this.lable = lable.trim(); 
  32. //String.trim()截去字符串开头和末尾的空白 
  33. return flag; 
  34. } 
  35.  
  36. public double getRadius() 
  37. { 
  38. return radius; 
  39. } 
  40.  
  41. public void setRadius(double radius) 
  42. { 
  43. this.radius = radius; 
  44. } 
  45.  
  46. public double getHeight() 
  47. { 
  48. return height; 
  49. } 
  50.  
  51. public void setHeight(double height) 
  52. { 
  53. this.height = height; 
  54. } 
  55.  
  56. //返回圆柱底面直径 
  57. public double diameter() 
  58. { 
  59. return radius * 2; 
  60. } 
  61.  
  62. //返回圆柱底面周长 
  63. public double circumference() 
  64. { 
  65. return diameter() * Math.PI; 
  66. } 
  67.  
  68. //返回 表面积 = 圆柱底面积×2 + 底面周长×高 
  69. public double area() 
  70. { 
  71. return Math.PI * radius * radius * 2 
  72. + circumference() * height; 
  73. } 
  74.  
  75. //返回 圆柱底体积 
  76. public double volumn() 
  77. { 
  78. return Math.PI * radius * radius * height; 
  79. } 
  80.  
  81. @Override 
  82. public String toString() 
  83. { 
  84. String output = null; 
  85. DecimalFormat df = new DecimalFormat("#,##0.0##"); 
  86. output = lable 
  87. + " is a cylinder with radius = " + df.format(radius) 
  88. + " units and height = " + df.format(height) 
  89. + " units, " 
  90. + "\nwhich has diameter = " + df.format(diameter()) 
  91. + " units, circumference = " + df.format(circumference()) 
  92. + " units, " 
  93. + "\narea = " + df.format(area()) 
  94. + " square units, and volume = " + df.format(volumn()) 
  95. + " cubic units.\n"; 
  96. return output; 
  97. } 
  98.  
  99. public static void main(String[] args) 
  100. { 
  101. Cylinder c1 = new Cylinder("Small Example", 4.0, 10.0); 
  102. Cylinder c2 = new Cylinder("Medium Example", 22.1, 30.6); 
  103. Cylinder c3 = new Cylinder("Large Example", 100.0, 200.0); 
  104. c1.setLable(""); 
  105. System.out.println(c1); 
  106. System.out.println(c2); 
  107. System.out.println(c3); 
  108. } 
  109. } 
  110.  

7.2 CylinderList.java

  1. package week7; 
  2.  
  3. import java.text.DecimalFormat; 
  4. import java.util.ArrayList; 
  5.  
  6. /** 
  7. * 7.2 CylinderList类  
  8. */ 
  9. public class CylinderList 
  10. { 
  11. private String listName; 
  12. private ArrayList<Cylinder> cList; 
  13.  
  14. CylinderList(String listName, ArrayList<Cylinder> cList) 
  15. { 
  16. this.listName = listName; 
  17. this.cList = cList; 
  18. } 
  19.  
  20. //返回一个代表几何名字的字符串 
  21. public String getName() 
  22. { 
  23. return listName; 
  24. } 
  25.  
  26. //返回代表集合中Cylinder对象的个数 
  27. public int numberOfCylinders() 
  28. { 
  29. return cList.size(); 
  30. } 
  31.  
  32. //返回 所有的Cylinder对象的 高 的和 
  33. public double totalHeight() 
  34. { 
  35. double totalHeight = 0; 
  36. for (Cylinder cylinder : cList) 
  37. { 
  38. totalHeight += cylinder.getHeight(); 
  39. } 
  40. return totalHeight; 
  41. } 
  42.  
  43. //返回 所有的Cylinder对象的 圆柱底面直径 的和 
  44. public double totalDiameter() 
  45. { 
  46. double totalDiameter = 0; 
  47. for (Cylinder cylinder : cList) 
  48. { 
  49. totalDiameter += cylinder.diameter(); 
  50. } 
  51. return totalDiameter; 
  52. } 
  53.  
  54. //返回 所有的Cylinder对象的 面积 之和 
  55. public double totalArea() 
  56. { 
  57. double totalArea = 0; 
  58. for (Cylinder cylinder : cList) 
  59. { 
  60. totalArea += cylinder.area(); 
  61. } 
  62. return totalArea; 
  63. } 
  64.  
  65. //返回 所有的Cylinder对象的 体积 之和 
  66. public double totalVolume() 
  67. { 
  68. double totalVolume = 0; 
  69. for (Cylinder cylinder : cList) 
  70. { 
  71. totalVolume += cylinder.volumn(); 
  72. } 
  73. return totalVolume; 
  74. } 
  75.  
  76. //返回 所有的Cylinder对象 面积 的 平均值 
  77. public double averageArea() 
  78. { 
  79. double averageArea = 0; 
  80. if (cList.size()>0) 
  81. { 
  82. averageArea = totalArea()/cList.size(); 
  83. } 
  84. return averageArea; 
  85. } 
  86.  
  87. //返回 所有的Cylinder对象 体积 的 平均值 
  88. public double averageVolume() 
  89. { 
  90. double averageVolume = 0; 
  91. if (cList.size()>0) 
  92. { 
  93. averageVolume = totalVolume()/cList.size(); 
  94. } 
  95. return averageVolume; 
  96. } 
  97.  
  98. //返回 集合的名字及集合中每一个对象的toString方法 
  99. public String toString() 
  100. { 
  101. String output = "\n" + listName + "\n\n"; 
  102. for (Cylinder cylinder : cList) 
  103. { 
  104. output += (cylinder.toString() + "\n");  
  105. } 
  106. return output; 
  107. } 
  108.  
  109. //返回 集合的名字及Cylinder对象个数, 
  110. //总面积,总体积,平均面积及平均体积 
  111. public String summaryInfo() 
  112. { 
  113. String output = null; 
  114. DecimalFormat df = new DecimalFormat("#,##0.0##"); 
  115. output = "-----" + listName + " Summary-----" 
  116. + "\nNimber of Cylinders: " + numberOfCylinders() 
  117. + "\nTotal Area: " + df.format(totalArea()) 
  118. + "\nTotal Volume: " + df.format(totalVolume()) 
  119. + "\nTotal Height: " + df.format(totalHeight()) 
  120. + "\nTotal Diameter: " + df.format(totalDiameter()) 
  121. + "\nAverage Area: " + df.format(averageArea()) 
  122. + "\nAverage Volume: " + df.format(averageVolume()); 
  123. return output; 
  124. } 
  125. } 

7.3 CylinderListApp 测试类

  1. package week7; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileNotFoundException; 
  5. import java.util.ArrayList; 
  6. import java.util.Scanner; 
  7.  
  8. /** 
  9. * 7.3 CylinderListApp 测试类 
  10. * (a) 打开用户输入的文件并读取第一行作为集合的名字;之后读取其他行, 
  11. * 依次生成Cylinder对象,最后生成CylinderList对象。 
  12. * (b) 输出CylinderList对象(调用toString方法),之后空一行, 
  13. * (c) 输出CylinderList对象的汇总信息(调用summaryInfo方法) 
  14. * 注意: 
  15. * 1)如果输入文件名后出现错误称找不到文件,此时可输出绝对路径 
  16. * 2)读取文件第一行作为集合名称后,使用以scanFile.hasNext() 
  17. * 为条件的while循环反复读入三行,然后创建Cylinder对象 
  18. * 3)输出结果必须与下面测试输出的结果完全一致 
  19. */ 
  20. public class CyliderListApp 
  21. { 
  22. public static void main(String[] args) throws FileNotFoundException 
  23. { 
  24. String lable; 
  25. double radius; 
  26. double height; 
  27. Scanner scan0 = new Scanner(System.in); 
  28. Scanner scan1 = null; 
  29. Scanner inputStream = null; 
  30.  
  31. ArrayList<Cylinder> cList = new ArrayList<>(10); 
  32. CylinderList cylinderList = null;//new CylinderList(listName, cList) 
  33.  
  34. System.out.print("Enter file name: "); 
  35. String fileName = scan0.nextLine(); // cylinder_0.dat 
  36. scan0.close(); 
  37.  
  38. File file = new File(fileName); 
  39. if(file.exists()) 
  40. { 
  41. inputStream = new Scanner(file); 
  42.  
  43. String listName = inputStream.nextLine(); 
  44. while (inputStream.hasNextLine()) 
  45. { 
  46. String line = inputStream.nextLine(); 
  47. //使用逗号分隔line,例:Small Example, 4.0, 10.0 
  48. scan1 = new Scanner(line); 
  49. scan1.useDelimiter(","); 
  50. if (scan1.hasNext()) 
  51. { 
  52. lable = scan1.next(); 
  53. radius = Double.parseDouble(scan1.next()); 
  54. height = Double.parseDouble(scan1.next()); 
  55. //创建Cylinder对象并加入ArrayList<Cylinder>中 
  56. cList.add(new Cylinder(lable, radius, height)); 
  57. } 
  58. scan1.close(); //就近原则,以免出现空指针 
  59. } 
  60. inputStream.close(); 
  61. //初始化CylinderList对象 
  62. cylinderList = new CylinderList(listName, cList); 
  63. System.out.print(cylinderList.toString()); 
  64. System.out.println(cylinderList.summaryInfo()); 
  65. } 
  66. else 
  67. { 
  68. System.out.println(file.getAbsolutePath()); 
  69. } 
  70. } 
  71. } 
  72.  

运行结果:

技术分享

技术分享

8 Cylinder(三)

-------------------------2016-11-**更新

//wiating for update... 
//
//run
//

9 Cylinder(四)

-------------------------2016-11-**更新

//wiating for update... 
//
//run
//

java-7311练习(下)

原文:http://www.cnblogs.com/oucbl/p/6079156.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!