说明:
编一程序:可操作界面的那种 对红球自定规则: 可设置 排除4位以上的号相连,除去5个偶数或5个奇数的红色球, 可设置 不要的号码和 必要的号码, 要设置 最大号和最小号之差大于15 还要设置一个规则 排除前面几期出现的组,手工输入以往的中奖号码即可,要能设置重复个数N,不能出现N个与以往号码相同
------------------------------------------------------------- “双色球”彩票投注区分为红色球号码区和蓝色球号码区。 “双色球”每注投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1--33中选择;蓝色球号码从1--16中选择。 单式投注是从红色球号码中选择6个号码,从蓝色球号码中选择1个号码,组合为一注投注号码的投注。
--------------------------------------------------------------------------------------
package luckyBall; import java.awt.*;
import java.awt.event.*;
import java.util.Arrays; public class LuckyBall {
public static void main(String args[]) {
new MyFrame("Good Luck");
}
} class gFrame extends Frame {
Button choose;
TextField lastNumText, dupNumText, mustChooseText, cannotChooseText,
resultText; gFrame(String s) {
super(s); setLayout(new GridLayout(6, 2, 10, 10));
setBackground(new Color(204, 204, 255)); Label lastNumLabel = new Label("往期中奖号码:");
lastNumText = new TextField(); Label dupNumLabel = new Label("不可重复号码数:");
dupNumText = new TextField(); Label mustChooseLabel = new Label("设置必选号码:");
mustChooseText = new TextField(); Label cannotChooseLabel = new Label("设置不选号码:");
cannotChooseText = new TextField(); Label kong = new Label();
choose = new Button("选号"); Label result = new Label("选号结果:");
resultText = new TextField(); lastNumText.addActionListener(new MyMonitor());
dupNumText.addActionListener(new MyMonitor());
mustChooseText.addActionListener(new MyMonitor());
cannotChooseText.addActionListener(new MyMonitor());
choose.addActionListener(new MyMonitor());
// manual.addActionListener(new MyMonitor()); add(lastNumLabel);
add(lastNumText);
add(dupNumLabel);
add(dupNumText);
add(mustChooseLabel);
add(mustChooseText);
add(cannotChooseLabel);
add(cannotChooseText);
add(kong);
add(choose);
// add(manual);
add(result);
add(resultText); pack();
setVisible(true); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(-1);
}
});
} private class MyMonitor implements ActionListener {
String[] lastNum = new String[7];
String[] mustChoose = new String[7];
String[] cannotChoose = new String[15];
int dupNum = 0; /**
* 产生双色球的七个号码
*
* @param ball
* @return
*/
public int[] createBall(int[] ball, String[] lastNum, int dupNum,
String[] mustChoose, String[] cannotChoose) { for (int i = 0; i < ball.length; i++) {
// 默认最后一个号码保留给蓝色球
if (i < ball.length - 1) {
// 如果是红色球,则验证每个产生的号码是否已经存在,并验证是否符合规则。
ball = this.redBall(ball, i, lastNum, dupNum,
mustChoose, cannotChoose); } else {
// 如果是蓝色球,直接产生1--16之间的随机数
ball[i] = (int) Math.round(Math.random() * 15 + 1);
}
} // 判断两组号码中有几个相同的号码
if (lastNum.length > 0) {
for (int i = 0; i < ball.length-1; i++) {
int count4 = 0;
for (int j = 0; j < lastNum.length-1; j++) {
if (lastNum[j] != null) {
int p = ball[i];
int q = Integer.parseInt(lastNum[j]);
if (q == p) {
count4++;
}
}
}
if (count4 >= dupNum) {
ball = this.redBall(ball, i, lastNum, dupNum,
mustChoose, cannotChoose);
}
}
} if (mustChooseText.getText().length() > 0) { for (int k = 0; k < mustChoose.length; k++) {
ball[k] = Integer.parseInt(mustChoose[k]); } for (int m = mustChoose.length; m < ball.length; m++) {
int[] ball2 = new int[6 - m];
ball2 = this.redBall(ball2, m, lastNum, dupNum,
mustChoose, cannotChoose);
for (int k = 0; k < ball2.length; k++) {
ball[m] = ball2[k];
}
} return ball; } // 产生号码完毕,返回这注产生的号码。
return ball;
} /**
* 得到无重复的、符合规则的红色号码序列 每次只产生一个红色号码,并验证此号码是否已经存在,是否符合规则。
* 如果存在或者不符合规则,重新产生号码,直到没有重复号码。
*
* @param ball
* @param index
* 产生红色球数组的下标,即第几个号码。
* @return
*/
public int[] redBall(int[] ball, int index, String[] lastNum,
int dupNum, String[] mustChoose, String[] cannotChoose) { // 产生一个1--33的随机数
int random = (int) Math.round(Math.random() * 32 + 1); int count1 = 0;
int count2 = 0;
int count3 = 1; boolean flag = false;
boolean minus = false; while (true) {
int i = 0;
for (; i < index; i++) {
Arrays.sort(ball, 0, index); // 判断是否是5个奇数或5个偶数
if (ball[i] % 2 == 0) {
count1++; // 偶数个数
}
if (ball[i] % 2 == 1) {
count2++; // 奇数个数
} // 判断连续3个以上的数
if(i>=2){
int m = ball[i];
int n = ball[i + 1];
if (n == m + 1) {
count3++;
}
} // 判断最大数与最小数之差大于15
if (index == 5) {
if (random - ball[0] <= 15) {
minus = true;
}
} // 排除不能选的数字
if (cannotChoose.length > 0) {
for (int j = 0; j < cannotChoose.length; j++) {
if ((cannotChoose[j] != "")&& random == Integer.parseInt(cannotChoose[j])) {
flag = true;
}
}
} if ((count1 >= 5)&&(random%2==0) || (count2 >= 5)&&(random%2 == 1)
|| (count3 >= 3)&&(random == ball[i]+1)
|| minus == true || flag == true
|| random == ball[i]) {
// 重新产生号码
random = (int) Math.round(Math.random() * 32 + 1);
// 并且跳出for循环,进入while循环
i = index + 1;
}
}
// 如果验证完了所有号码,那把这个号码插入数组,并返回
if (i == index) {
ball[index] = random; flag = false;
minus = false;
count1 = 0;
count2 = 0;
count3 = 1; return ball;
} }
} public void actionPerformed(ActionEvent e) {
/*
String[] lastNum = new String[7];
String[] mustChoose = new String[7];
String[] cannotChoose = new String[15];
int dupNum = 0;
*/ if (e.getSource() == lastNumText) {
lastNum = lastNumText.getText().split("\\w");
}
if (e.getSource() == dupNumText) {
dupNum = Integer.parseInt(dupNumText.getText());
}
if (e.getSource() == mustChooseText) {
mustChoose = mustChooseText.getText().split("\\w");
}
if (e.getSource() == cannotChooseText) {
cannotChoose = cannotChooseText.getText().split("\\w");
} if (e.getSource() == choose) {
int[] result = new int[7]; result = createBall(result, lastNum, dupNum, mustChoose,
cannotChoose); for (int i = 0; i < result.length; i++)
resultText.setText(result[i] + " "); }
}
} } 总是出错,不能生成满足规则的号码,请各位指点~
编一程序:可操作界面的那种 对红球自定规则: 可设置 排除4位以上的号相连,除去5个偶数或5个奇数的红色球, 可设置 不要的号码和 必要的号码, 要设置 最大号和最小号之差大于15 还要设置一个规则 排除前面几期出现的组,手工输入以往的中奖号码即可,要能设置重复个数N,不能出现N个与以往号码相同
------------------------------------------------------------- “双色球”彩票投注区分为红色球号码区和蓝色球号码区。 “双色球”每注投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1--33中选择;蓝色球号码从1--16中选择。 单式投注是从红色球号码中选择6个号码,从蓝色球号码中选择1个号码,组合为一注投注号码的投注。
--------------------------------------------------------------------------------------
package luckyBall; import java.awt.*;
import java.awt.event.*;
import java.util.Arrays; public class LuckyBall {
public static void main(String args[]) {
new MyFrame("Good Luck");
}
} class gFrame extends Frame {
Button choose;
TextField lastNumText, dupNumText, mustChooseText, cannotChooseText,
resultText; gFrame(String s) {
super(s); setLayout(new GridLayout(6, 2, 10, 10));
setBackground(new Color(204, 204, 255)); Label lastNumLabel = new Label("往期中奖号码:");
lastNumText = new TextField(); Label dupNumLabel = new Label("不可重复号码数:");
dupNumText = new TextField(); Label mustChooseLabel = new Label("设置必选号码:");
mustChooseText = new TextField(); Label cannotChooseLabel = new Label("设置不选号码:");
cannotChooseText = new TextField(); Label kong = new Label();
choose = new Button("选号"); Label result = new Label("选号结果:");
resultText = new TextField(); lastNumText.addActionListener(new MyMonitor());
dupNumText.addActionListener(new MyMonitor());
mustChooseText.addActionListener(new MyMonitor());
cannotChooseText.addActionListener(new MyMonitor());
choose.addActionListener(new MyMonitor());
// manual.addActionListener(new MyMonitor()); add(lastNumLabel);
add(lastNumText);
add(dupNumLabel);
add(dupNumText);
add(mustChooseLabel);
add(mustChooseText);
add(cannotChooseLabel);
add(cannotChooseText);
add(kong);
add(choose);
// add(manual);
add(result);
add(resultText); pack();
setVisible(true); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(-1);
}
});
} private class MyMonitor implements ActionListener {
String[] lastNum = new String[7];
String[] mustChoose = new String[7];
String[] cannotChoose = new String[15];
int dupNum = 0; /**
* 产生双色球的七个号码
*
* @param ball
* @return
*/
public int[] createBall(int[] ball, String[] lastNum, int dupNum,
String[] mustChoose, String[] cannotChoose) { for (int i = 0; i < ball.length; i++) {
// 默认最后一个号码保留给蓝色球
if (i < ball.length - 1) {
// 如果是红色球,则验证每个产生的号码是否已经存在,并验证是否符合规则。
ball = this.redBall(ball, i, lastNum, dupNum,
mustChoose, cannotChoose); } else {
// 如果是蓝色球,直接产生1--16之间的随机数
ball[i] = (int) Math.round(Math.random() * 15 + 1);
}
} // 判断两组号码中有几个相同的号码
if (lastNum.length > 0) {
for (int i = 0; i < ball.length-1; i++) {
int count4 = 0;
for (int j = 0; j < lastNum.length-1; j++) {
if (lastNum[j] != null) {
int p = ball[i];
int q = Integer.parseInt(lastNum[j]);
if (q == p) {
count4++;
}
}
}
if (count4 >= dupNum) {
ball = this.redBall(ball, i, lastNum, dupNum,
mustChoose, cannotChoose);
}
}
} if (mustChooseText.getText().length() > 0) { for (int k = 0; k < mustChoose.length; k++) {
ball[k] = Integer.parseInt(mustChoose[k]); } for (int m = mustChoose.length; m < ball.length; m++) {
int[] ball2 = new int[6 - m];
ball2 = this.redBall(ball2, m, lastNum, dupNum,
mustChoose, cannotChoose);
for (int k = 0; k < ball2.length; k++) {
ball[m] = ball2[k];
}
} return ball; } // 产生号码完毕,返回这注产生的号码。
return ball;
} /**
* 得到无重复的、符合规则的红色号码序列 每次只产生一个红色号码,并验证此号码是否已经存在,是否符合规则。
* 如果存在或者不符合规则,重新产生号码,直到没有重复号码。
*
* @param ball
* @param index
* 产生红色球数组的下标,即第几个号码。
* @return
*/
public int[] redBall(int[] ball, int index, String[] lastNum,
int dupNum, String[] mustChoose, String[] cannotChoose) { // 产生一个1--33的随机数
int random = (int) Math.round(Math.random() * 32 + 1); int count1 = 0;
int count2 = 0;
int count3 = 1; boolean flag = false;
boolean minus = false; while (true) {
int i = 0;
for (; i < index; i++) {
Arrays.sort(ball, 0, index); // 判断是否是5个奇数或5个偶数
if (ball[i] % 2 == 0) {
count1++; // 偶数个数
}
if (ball[i] % 2 == 1) {
count2++; // 奇数个数
} // 判断连续3个以上的数
if(i>=2){
int m = ball[i];
int n = ball[i + 1];
if (n == m + 1) {
count3++;
}
} // 判断最大数与最小数之差大于15
if (index == 5) {
if (random - ball[0] <= 15) {
minus = true;
}
} // 排除不能选的数字
if (cannotChoose.length > 0) {
for (int j = 0; j < cannotChoose.length; j++) {
if ((cannotChoose[j] != "")&& random == Integer.parseInt(cannotChoose[j])) {
flag = true;
}
}
} if ((count1 >= 5)&&(random%2==0) || (count2 >= 5)&&(random%2 == 1)
|| (count3 >= 3)&&(random == ball[i]+1)
|| minus == true || flag == true
|| random == ball[i]) {
// 重新产生号码
random = (int) Math.round(Math.random() * 32 + 1);
// 并且跳出for循环,进入while循环
i = index + 1;
}
}
// 如果验证完了所有号码,那把这个号码插入数组,并返回
if (i == index) {
ball[index] = random; flag = false;
minus = false;
count1 = 0;
count2 = 0;
count3 = 1; return ball;
} }
} public void actionPerformed(ActionEvent e) {
/*
String[] lastNum = new String[7];
String[] mustChoose = new String[7];
String[] cannotChoose = new String[15];
int dupNum = 0;
*/ if (e.getSource() == lastNumText) {
lastNum = lastNumText.getText().split("\\w");
}
if (e.getSource() == dupNumText) {
dupNum = Integer.parseInt(dupNumText.getText());
}
if (e.getSource() == mustChooseText) {
mustChoose = mustChooseText.getText().split("\\w");
}
if (e.getSource() == cannotChooseText) {
cannotChoose = cannotChooseText.getText().split("\\w");
} if (e.getSource() == choose) {
int[] result = new int[7]; result = createBall(result, lastNum, dupNum, mustChoose,
cannotChoose); for (int i = 0; i < result.length; i++)
resultText.setText(result[i] + " "); }
}
} } 总是出错,不能生成满足规则的号码,请各位指点~
我所关心的是你的算法。
你可以参考一下这里:http://www.javaeye.com/topic/211491
public static void main(String args[]) {
new MyFrame("Good Luck");
} public static void main(String args[]) {中间多数是Console.writeline()}
你在这中new 一个对象搞不懂为啥