首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

找了半天没找出来是哪的有关问题,哪位大大看看

2012-01-20 
找了半天没找出来是哪的问题,哪位大大看看啊写得一个双色球线程前6个数字要不相等的package 双色球import

找了半天没找出来是哪的问题,哪位大大看看啊
写得一个双色球线程 前6个数字要不相等的

package 双色球;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class ChooseNumber extends JFrame{
  MyLabel x[]=new MyLabel[6];
  MyLabel1 y=new MyLabel1();
  JButton control;
   
  public ChooseNumber(String title){
  super(title);
  JPanel disp=new JPanel();
  disp.setLayout(new FlowLayout());
  for(int i=0;i<6;i++){
  x[i]=new MyLabel();
  x[i].setForeground(Color.red);
  disp.add(x[i]);
  new Thread(x[i]).start();
  if(i==5)
  {
  y.setForeground(Color.blue);
  disp.add(y);
  new Thread(y).start();
  }
  }
  add("Center",disp);
  control=new JButton("停止");
  add("South",control);
  setSize(250,100);
  setVisible(true);
  Toolkit kit =Toolkit.getDefaultToolkit();
  Dimension s=kit.getScreenSize();
  setLocation(s.width/2-125,s.height/2-200);
  control.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {

  String str=control.getText();
  if(str.equals("停止"))
  {
  int []a=new int[6]; //从这里开始定义一个数组存入6个不形同的值
  boolean b;
  for(int i=0;i<6;i++)
  {
  a[i]=(int)(Math.random()*32)+1;
  if(i>0)
  {
  b=true;
  int n=0;
  while(b)
  {
  if(a[i]!=a[n])
  {
  n++;
  if(n==i)
  b=false;
  }
  else a[i]=(int)(Math.random()*32)+1;
  }
  }
  }
  for(int i=0;i<6;i++)
  {
  x[i].value=a[i]; // 然后在这里将值传出显示出来
  x[i].stop=true;
  if(i==5)
  y.stop=true;
  }
  control.setText("开始");  
  }
   
  if(str.equals("开始"))
  {
  for(int i=0;i<6;i++)
  {
  x[i].stop=false;
  new Thread(x[i]).start();
  if(i==5)
  {
  y.stop=false;
  new Thread(y).start();
  }
  }
  control.setText("停止");  


  }
  }
  });
  }
}


package 双色球;

import java.awt.Label;

/**
 *
 * @author Administrator
 */
public class MyLabel extends Label implements Runnable{
  int value;
  boolean stop=false;
  public MyLabel()
  {
  super("number");
  value=0;
  }
  public void run(){
  for(;;)
  {
  value=(int)(Math.random()*32)+1;
  setText(Integer.toString(value));
  try {
  Thread.sleep(100);
  } catch (Exception e) {
  }
  if(stop)
  {
  break;
  }
  }
  }  
}

package 双色球;

import java.awt.Label;

/**
 *
 * @author Administrator
 */
public class MyLabel1 extends Label implements Runnable{
  int value;
  boolean stop=false;
  public MyLabel1()
  {
  super("number");
  value=0;
  }
  public void run(){
  for(;;)
  {
  value=(int)(Math.random()*16)+1;
  setText(Integer.toString(value));
  try {
  Thread.sleep(100);
  } catch (Exception e) {
  }
  if(stop)
  {
  break;
  }
  }
  }
}


package 双色球;

import javax.swing.JFrame;

/**
 *
 * @author Administrator
 */
public class Main {

  /**
  * @param args the command line arguments
  */
  public static void main(String[] args) {
  new ChooseNumber("选号程序").setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

因为不知道线程该怎么写能生成不冲突的值,所以投机取巧设定另一不重复的数组传值。但是这样还是会出现重复值。
我想是那个数组有问题,然后就又把那个数组方法单独写出来测试

package javaapplication6;

/**
 *
 * @author Administrator
 */
public class JavaApplication6 {

  /**
  * @param args the command line arguments
  */
  public static void main(String[] args) {
  int []a=new int[6];
  for(int i=0;i<6;i++)
  {
  a[i]=(int)(Math.random()*32)+1;
  if(i>0)
  {
  boolean b=true;
  int n=0;
  while(b)
  {
  if(a[i]!=a[n])
  {
  n++;
  if(n==i)
  b=false;
  }
  else a[i]=(int)(Math.random()*32)+1;
  }
  }
  System.out.print(a[i]+" ");
  }
}
}


然后运行了很多次都没出现重复值、、、、
何解?


[解决办法]

Java code
for(int i=0;i<6;i++)                    {                        x[i].stop=true;                        if(i==5)                            y.stop=true;                    }                    for(int i=0;i<6;i++)                    {                        x[i].value=a[i]; // 然后在这里将值传出显示出来                        x[i].setText(Integer.toString(a[i]));                    } 

热点排行