写得一个双色球线程 前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]+" ");
}
}
}
然后运行了很多次都没出现重复值、、、、
何解?
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]+" ");
}
}
}
然后运行了很多次都没出现重复值、、、、
何解?
{
a[i]=(int)(Math.random()*32)+1;
//可以改成List去做,这样比较方便,直接有contains来判断是否有重复值
for(int j=0;j < i;j++){
if(a[i]==a[j]){//找到一个相同的就要产生新的值
a[i]=(int)(Math.random()*32)+1;
j=-1;//继续重头判断
}
}
System.out.println(a[i]);
}
这样的情况下,红球有可能与前边的重复
a[i]=(int)(Math.random()*32)+1;
n=0;
}产生一个新的值的时候,应该重新从头开始判断.也就是让n=0;
不知道怎么回事、、、
可是怎么改呢
我试了试 先在循环里用 x[i].stop=true;使该线程停止 然后再x[i].value=a[i];将值传递、、
还是不行、、、不知道这样写对不对、、、
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]));
}
这一句不可少
x[i].setText(Integer.toString(a[i]));
public void run(){
for(;;)
{
try {
Thread.sleep(100);
} catch (Exception e) {
}
if(stop)
{
break;
}
value=(int)(Math.random()*32)+1;
setText(Integer.toString(value));
}
}
这边线程要换一下