日期:2014-05-20 浏览次数:20808 次
import java.util.Random; import java.util.Set; import java.util.TreeSet; public class SSQ { public static void main(String[] args) { // 上期特等奖号码 int[] red = { 7, 13, 15, 17, 19, 24 }; int blue = 11; Set<Integer> set1 = new TreeSet<Integer>(); Set<Integer> set = new TreeSet<Integer>(); Random random = new Random(); // 得到上期特等奖的数组 for (Integer a : red) { set1.add(a); } // 随机产生预测红球 while (set.size() != 6) { int randomValue = random.nextInt(33) + 1; set.add(randomValue); } // 随机产生预测蓝球 int randomValue = random.nextInt(16) + 1; String result = "红球:"; for (Integer i : set) { result += i + " , "; } result += " 蓝球:" + randomValue; System.out.println(result); } }
public class SSQ { public static void main(String[] args) { // 上期特等奖号码 int[] red = { 7, 13, 15, 17, 19, 24 }; int blue = 11; Set<Integer> set1 = new TreeSet<Integer>(); Set<Integer> set = new TreeSet<Integer>(); Random random = new Random(); // 得到上期特等奖的数组 for (Integer a : red) { set1.add(a); } while (true) { //加个While循环 // 随机产生预测红球 while (set.size() != 6) { int randomValue = random.nextInt(33) + 1; set.add(randomValue); } // 随机产生预测蓝球 int randomValue = random.nextInt(16) + 1; if (set.cotains(randomValue)) break; //如果红球有一个和蓝球相同则退出while循环 String result = "红球:"; for (Integer i : set) { result += i + " , "; } result += " 蓝球:" + randomValue; System.out.println(result); } } }
------解决方案--------------------
package com.sg; import java.util.Random; import java.util.Set; import java.util.TreeSet; public class SSQ { public static void main(String[] args) { // 上期特等奖号码 int[] red = { 7, 13, 15, 17, 19, 24 }; int blue = 11; Set<Integer> set1 = new TreeSet<Integer>(); Random random = new Random(); // 得到上期特等奖的数组 for (Integer a : red) { set1.add(a); } while(true) { Set<Integer> set = new TreeSet<Integer>(); // 随机产生预测红球 while (set.size() != 6) { int randomValue = random.nextInt(33) + 1; set.add(randomValue); } // 随机产生预测蓝球 int randomValue = random.nextInt(16) + 1; StringBuffer result = new StringBuffer("红球:"); for (Integer i : set) { result.append(i + " , "); } result.append(" 蓝球:" + randomValue); // 默认会调用StringBuffer的toString()方法 System.out.println(result); // 判断新生成篮球和红球是否与上一期的篮球和红球相等 //:由于set不重复,所遇两个set集合如果大小相同且其中一个包含另外一个的全部内容,可以认定两个集合内容相同 if(randomValue == blue && (set.size() == set1.size() && set1.containsAll(set))) { System.out.println("成功匹配!"); return; } } } }