java彩票系統
1. java澶х炲府鎴戠湅鐪嬩綔涓氾紝鍋氫竴涓25閫5鐨勫僵紲ㄦ満錛屾垜鍋氱殑綆椾笉綆楁瘮杈冪畝媧佺殑錛
鉶界劧綆媧侊紝浣嗕笉瑙勮寖錛屾渶濂芥槸鎸夎勮寖瑕佹眰璧幫紱渚嬪傦細杈撳嚭鐨25錛屾渶濂藉啓鎴愶細
System.out.print("褰╃エ鎬婚暱搴︿負錛"+ArrayNum.length+"\n"+"闅忔満鍙風爜涓猴細");
for(int i =0; i<25;i++) 鏈濂藉啓鎴愶細for(int i =0; i<arrayNum.length;i++)
2. java璇璦瀹炵幇涓涓鎽囧彿緋葷粺錛屼絾鏄鍙浠ュ唴閮ㄨ懼畾涓濂栧悕鍗曡繖涓鎬庝箞瀹炵幇錛
1銆佺畝鍗曟帶鍒跺彴紼嬪簭濡備笅錛屽傞渶鐣岄潰闇瑕佽嚜宸卞姞涓銆
package ;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
/**
* @author bufei
* @datetime 2020騫8鏈31鏃15:54:11
*/
public class YaoHao {
public static void main(String[] args) {
String xian = "= = = = = = = = =";
// 璧風偣 緇堢偣 濂栦釜鏁 鎸囧畾鐨勫彿鐮佷釜鏁
int start = 0, end = 0, prizeNum = 0, defaNum = 0;
System.out.println(xian);
System.out.println("嬈㈣繋浣跨敤xxx 鎶藉栫郴緇燂紒");
System.out.println(xian);
Scanner scanner = new Scanner(System.in);
System.out.println("璇瘋緭鍏ュ彿鐮佽寖鍥翠緥濡 1 10錛");
start = scanner.nextInt();
end = scanner.nextInt();
System.out.println("璇瘋緭鍏ュ栭」涓鏁幫細");
prizeNum = scanner.nextInt();
System.out.println("璇瘋緭鍏ユ寚瀹氫腑濂栧彿鐮佷釜鏁幫紝涓嶆寚瀹氳瘋緭鍏0錛");
defaNum = scanner.nextInt();
int[] defa = new int[defaNum];
if (defaNum != 0) {
System.out.println("璇瘋緭鍏ユ寚瀹氱殑涓濂栧彿鐮侊紝絀烘牸闅斿紑:");
for (int i = 0; i < defaNum; i++) {
defa[i] = scanner.nextInt();
}
}
System.out.println(xian);
System.out.println("鏈嬈℃娊濂栦腑濂栧彿鐮佷負錛");
for (int num : randomDraw(start, end, prizeNum, defa)) {
System.out.print(num+" ");
}
}
/**
* @param start 鎶藉栬寖鍥磋搗鐐
* @param end 鎶藉栧彿鐮佽寖鍥寸粓鐐
* @param prizeNum 涓濂栧彿鐮佷釜鏁
* @param defa 鎸囧畾涓濂栧彿鐮
* @return
*/
public static Set<Integer> randomDraw(int start, int end, int prizeNum, int[] defa) {
Set<Integer> set = new HashSet<>();
// 鏈鎸囧畾涓濂栧彿鐮
if (defa.length == 0) {
//闅忔満鎶 prizeNum 涓濂
while (set.size() < prizeNum) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
} else {
//鎸囧畾浜嗕腑濂栧彿鐮
//鎶婃寚瀹氱殑鍙風爜鍔犲叆榪涘幓
for (int num : defa) {
set.add(num);
}
//濡傛灉娌℃湁鍏ㄩ儴鎸囧畾 鍒欑戶緇鎶藉墿浣欑殑濂栭」
while (set.size() < prizeNum - defa.length) {
set.add(new Random().nextInt(end - start + 1) + start);
}
return set;
}
}
}
2銆佽繍琛屾晥鏋滃傚浘
鏈鎸囧畾涓濂栧彿鐮
3. 用java編寫一個彩票抽獎程序
我寫給你,如果是本專業,以後要多多學習。JDK 1.6
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class LuckyDrawn {
private JFrame f = new JFrame("Lucky Drawn");
private JButton btn = new JButton("Drawn");
private JTextField result = new JTextField("Result");
private static final int MIN = 1;
private static final int MAX = 30;
private static final int RESULT_COUNT = 7;
public LuckyDrawn(){
f.add(btn);
f.add(result);
result.setEditable(false);
btn.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
Random rand = new Random();
Set<Integer> set = new TreeSet<Integer>();
while(set.size() < RESULT_COUNT){
set.add(new Integer(rand.nextInt(MAX)+ 1));
}
StringBuffer sb = new StringBuffer("Lucky numbers are: ");
for (Iterator iter = set.iterator(); iter.hasNext();) {
sb.append(((Integer) iter.next()).intValue()).append(", ");
}
result.setText(sb.substring(0, sb.length() - 2).toString());
f.pack();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
f.setLayout(new GridLayout(2, 1));
f.setVisible(true);
f.setLocation(500, 200);
f.pack();
f.validate();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new LuckyDrawn();
}
}