在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java随机数应用——双色球机选

2016-7-29 15:45| 发布者: zhangjf| 查看: 645| 评论: 0

摘要: 应用随机数机选双色球 package cn.mxy.demo; import java.util.*; public class Lottery { static Random rd = new Random(); public static void getLottery(){ Sy ...
应用随机数机选双色球
  1. package cn.mxy.demo;
  2. import java.util.*;
  3. public class Lottery {
  4. static Random rd = new Random();
  5. public static void getLottery(){
  6. System.out.print("红球:");
  7. int m[] = redBall();
  8. Arrays.sort(m);
  9. for(int x = 0; x < m.length; x++){
  10. System.out.print(m[x] + " ");
  11. }
  12. System.out.print( "蓝秋:" + blueBall());
  13. }
  14. private static int[] redBall(){
  15. int r[] = new int[6];
  16. int foot = 0;
  17. while(foot < 6){
  18. int ran = rd.nextInt(33)+1 ;
  19. if(!isRepeat(r, ran)){
  20. r[foot ++] = ran;
  21. }
  22. }
  23. return r;
  24. }
  25. private static int blueBall(){
  26. return rd.nextInt(16)+1;
  27. }
  28. private static boolean isRepeat(int[] r, int i){
  29. for(int x = 0; x < r.length; x++){
  30. if(r[x] == i)
  31. return true;
  32. }
  33. return false;
  34. }
  35. }
复制代码
  1. package cn.mxy.demo;
  2. public class Test {
  3. public static void main(String args[]){
  4. Lottery.getLottery();
  5. }
  6. }
复制代码
上一篇:java字符串操作下一篇:nextLine

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 13:18

Copyright 2015-2025 djqfx

返回顶部