本帖最后由 i46614161 于 2009-08-28 00:37:18 编辑

解决方案 »

  1.   

    问题是while循环中怎么实现遍历啊?
      

  2.   

    ns.add(a.nextInt(33) + 1);
      

  3.   

    这个方法更好你要遍历ns吗,用Iterator
      

  4.   

    将生成的6个数先保存在数组里,进行遍历,如果遇到0,则将该数重新用nextInt(34)获得,直到这六个数全不为0为止。
    虽然不如ns.add(a.nextInt(33) +1)这个方法巧妙,但也符合楼主的要求!
      

  5.   

    自己查了下API,找到一种方法://调用Set集合里面的contains(Object o)方法。 (如果此 set 包含指定的元素,则返回 true)
    public class Traversing {
    Traversing(){}
    public boolean isTraversing(Set<Integer> set){//传入参数,如果有0,返回flase
    return !(set.contains(new Integer(0)));
    }
    }在main()方法里面用if(ns.size()==6&&pd.isTraversing(ns)) break;判断
      

  6.   

    while(true){
                ns.add(a.nextInt(34));
                if(ns.size()>=6&&!ns.contains(Integer(0)) ) break;        }    
      

  7.   

            while(true){
                           if(b.nextInt(17)!==0) break;
                }
            System.out.println(b.nextInt(17));楼主这个地方抽了两次蓝球阿
    而且红色的部分写法不对哦
      

  8.   

    package josephus.game;
    import java.util.*;
    import java.io.*;
    public class Radom {
    public static void main(String[] args)throws Exception{
    Set<Integer> ns = new TreeSet<Integer>();
    Traversing pd=new Traversing();
    Random a=new Random();
    OutputStream os=new FileOutputStream("ball.dat",true);
    ObjectOutputStream out=new ObjectOutputStream(os);
    System.out.print("红球"+" ");
    while(true){
    ns.add(a.nextInt(34));
    if(ns.size()==6&&pd.isTraversing(ns)) break;
    }
    System.out.print(ns+" ");
    System.out.print("蓝球  [");
    System.out.println(a.nextInt(16)+1+"]");
    out.writeObject(ns);
    out.close();
    }
    }程序已经修改成这个样子,运行后可以得到不包含0的结果。但是,发现每运行几次之后就有点小问题。有时会只打印“红球”两字程序就不动了,为什么?
    看图:
      

  9.   

    <a href="http://www.flickr.com/photos/34109822@N06/3862867951/" title="Flickr 上 七月逆流 的 Screenshot"><img src="http://farm3.static.flickr.com/2544/3862867951_f7f456ee5f_o.png" width="821" height="623" alt="Screenshot" /></a>
      

  10.   

    自己写了一个抽奖方法,仅供参考import java.util.Hashtable;
    import java.util.Random;
    import java.util.TreeSet;public class TestRandom1 { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Hashtable result = getRandom(6,1,34,17);
    System.out.println("no1 is:"+result.get("no1").toString());
    System.out.println("no2 is:"+result.get("no2").toString());
    }
            //参数part1->要抽出第一部分的个数,也就是抽的6个红球
             //参数part1->要抽出第二部分的个数,也就是抽的1个蓝球
             //参数arrange1->红球的抽取范围
             //参数arrange2->蓝球的抽取范围 static Hashtable getRandom(int part1,int part2,int arrange1,int arrange2){
    Hashtable<String ,TreeSet> result = new Hashtable<String,TreeSet>();
    Random r = new Random();

    TreeSet<Integer> no1 = new TreeSet<Integer>(); 
    while(no1.size()<part1){
    int temp = r.nextInt(arrange1);
    if(temp!=0){
    no1.add(temp);
    }
    }

    TreeSet<Integer> no2 = new TreeSet<Integer>(); 
    while(no2.size()<part2){
    int temp = r.nextInt(arrange2);
    if(temp!=0){
    no2.add(temp);
    }
    }

    result.put("no1", no1);
    result.put("no2", no2);

    return result;
    }
    }
    执行结果
    no1 is:[2, 3, 10, 14, 20, 24]   //红球
    no2 is:[7]                      //蓝球
      

  11.   

    楼主要是想输出到文件的话,这样写
    最好别用ObjectOutputStream,它主要用来序列化,保存对象的
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Hashtable result = getRandom(6,1,34,17);

    try {
    PrintWriter pw = new PrintWriter("c:/test/testRandom.txt");
    pw.println(result.get("no1").toString());
    pw.print(result.get("no2").toString());
    pw.flush();
    pw.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
      

  12.   


     while(true){
                ns.add(a.nextInt(34));
                
                if(ns.size()==6&&pd.isTraversing(ns)){
                 break;
                }else{
                 ns.remove(new Integer(0));
                }
            }    
    有0后要删掉0,不然你While死循环
      

  13.   

    在原来随机数的基础上加1,看《郭克华j2me实战教学视频教材》学到的,呵呵~
      

  14.   

    把红球的号码放一Set中,用
    int code=rand.nextInt(set.size());
    set.remove(code);上面的语句循环6次。选蓝球也一样的道理。
      

  15.   

    while(true){
                int i = a.nextInt(34));
                if(!= 0)
                ns.add(i);
                if(ns.size()==6) break;//问题在这里:怎么实现 “先随机生成6个数,然后依次判断每个元素是否为0,若所有元素值不为0,跳出while循环”这个方法?
            }    
    虽然有点笨, 应该好用,也不太啰唆  当然  +1 的办法 比较好
      

  16.   

    while(true){
                ns.add(a.nextInt(34));
                if(ns.size()==6&&pd.isTraversing(ns)) break;
            } 你每次都不先判断是否为0就往ns里面add,到了size等于6的时候且ns里面有0,就执行不了break,然后循环继续下去了,size不断增加且永远不会再等于6了,死循环出现了,当然也就停在了"红球".
      

  17.   


    if(ns.size()==6&&pd.isTraversing(ns))break;
    问题处在这个判断上。加入ns.size()==6成立而后面条件不成立 ,则你就错过了唯一可以跳出循环的机会,所以一旦ns里面有0,你的程序就进入死循环了。
      

  18.   

    ns.add(a.nextInt(33) +1); 
    最简单..
      

  19.   

    ns.add(a.nextInt(33)+1);
    就可以解决0的问题了
      

  20.   

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Random;public class shuangseqiu {
    public static void main(String[] args) {
    List<Integer> redballs = new ArrayList<Integer>();
    List<Integer> blueballs = new ArrayList<Integer>();

    for(int i = 1;i < 34;i++)
    redballs.add(i);
    for(int i = 1;i < 17;i++)
    blueballs.add(i);

    Random rd = new Random();
    int thisredball = 0;
    List<Integer> rdredballs = new ArrayList<Integer>();
    int rdblueball = rd.nextInt(blueballs.size())+1;
    for(int i = 0;i < 6;i++){
    thisredball = rd.nextInt(redballs.size())+1;
    rdredballs.add(redballs.get(thisredball-1));
    redballs.remove(thisredball-1);
    }
    Collections.sort(rdredballs);
    System.out.print("红球 : ");
    for(int i = 0;i < rdredballs.size();i++){
    int iredball = rdredballs.get(i);
    String sredball = null;
    if(iredball < 10)
    sredball = "0"+String.valueOf(iredball);
    else sredball = String.valueOf(iredball);
    System.out.print(sredball + " ");
    }
    String sblueball = null;
    if(rdblueball < 10)
    sblueball = "0"+String.valueOf(rdblueball);
    else sblueball = String.valueOf(rdblueball);
    System.out.println("蓝球 : " + sblueball);

    }}
      

  21.   

    双色球问题:
    我的主程序是这样的,希望对你有些帮助:
    package test.arrays.doublecolorball;
    import java.util.*;
    public class DoublecolorBall {
    public static void main(String[] args) {
    String [] pool={"01","02","03","04","05","06","07",
                                        "08","09","10","11","12","13","14","15","16","17","18","19",
                                        "20","21","22","23","24","25","26","27","28","29","30","31","32","33"};
    boolean[] used=new boolean[pool.length];
    String [] balls=new String[6];
    Random ran=new Random();
    int i=0;
    while(true){//随机在pool数组中选择球,boolean为true则说明重复,跳出选择重新选。
    int index=ran.nextInt(pool.length);
    if(used[index]){
    continue;
    }
    balls[i++]=pool[index];
    used[index]=true;
    if(i==balls.length){
    break;
    }
    }
    Arrays.sort(balls);
    balls=Arrays.copyOf(balls, balls.length+1);
    balls[balls.length-1]=pool[ran.nextInt(16)];
    System.out.println(Arrays.toString(balls));//数组中,前六个数为红球,最后一位是蓝球。
    }
    }