publicstaticvoidmain(String[]args){
StringBuilderbuffer=newStringBuilder();
//title
for(inti=1;i=5;i++){
bufferappend(i+;;t;+10i+;;t;+100i+;;t;+1000i+;;n;);
Systemoutprintln(buffertoString());
}你是要java可视化的还是控制台的啊?
控制台的话你可以用循环来取中奖号,将其存入list或set里(最好是set,因为set本来就不会重复)。如果用list存就另外写一个循环方法,来挨个判定该元素是不是已经在list中存在了,如果存在,就让选号的再加选一次,如果不重复,就放到list里去。ok
我写给你,如果是本专业,以后要多多学习。JDK 16
import javaawtGridLayout;
import javaawteventMouseEvent;
import javaawteventMouseListener;
import javautilIterator;
import javautilRandom;
import javautilSet;
import javautilTreeSet;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJTextField;
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(){
fadd(btn);
fadd(result);
resultsetEditable(false);
btnaddMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
Random rand = new Random();
Set<Integer> set = new TreeSet<Integer>();
while(setsize() < RESULT_COUNT){
setadd(new Integer(randnextInt(MAX)+ 1));
}
StringBuffer sb = new StringBuffer("Lucky numbers are: ");
for (Iterator iter = setiterator(); iterhasNext();) {
sbappend(((Integer) iternext())intValue())append(", ");
}
resultsetText(sbsubstring(0, sblength() - 2)toString());
fpack();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
fsetLayout(new GridLayout(2, 1));
fsetVisible(true);
fsetLocation(500, 200);
fpack();
fvalidate();
fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
public static void main(String[] args) {
new LuckyDrawn();
}
}
优先级规则使高等奖尽量在后期抽出
import javautilLinkedList;import javautilList;
public class GetGift {
// 奖品仓库
private List<Gift> gifts = new LinkedList<Gift>();
public GetGift() {
// 生成一堆奖品放进奖品仓库
// 一等奖一个优先级1,二等奖两个优先级2。。。20等奖20个优先级20
for (int i = 1; i <= 20; i++) {
GiftType gt = new GiftType(i + "等奖", i, i);
for (int j = 1; j <= i; j++) {
giftsadd(new Gift(i + "等奖——第" + j + "号", gt));
}
}
}
// 抽奖
public synchronized Gift getGift() {
int randomNumber = (int) (Mathrandom() total());
int priority = 0;
for (Gift g : gifts) {
priority += ggetType()getPriority();
if (priority >= randomNumber) {
// 从奖品库移出奖品
giftsremove(g);
return g;
}
}
// 抽奖次数多于奖品时,没有奖品
return null;
}
/
@param args
/
public static void main(String[] args) {
GetGift gg = new GetGift();
// 一共生成210个奖品,抽210次,多抽显示null
for (int i = 0; i < 210; i++) {
Systemoutprintln(gggetGift());
}
}
// 计算总优先级,内部使用
private int total() {
int result = 0;
for (Gift g : gifts) {
result += ggetType()getPriority();
}
return result;
}
}
// 记录奖品的信息
// 如1等奖共1个,优先级为1最难抽
class GiftType {
// 名字(如1等奖)
private String name;
// 这种奖品的数量,数量越大越容易抽到
private int quantity;
// 这种奖品的优先级,最小为1,数越大越容易抽到
private int priority;
public GiftType(String name, int quantity, int priority) {
thisname = name;
thisquantity = quantity;
thispriority = priority;
}
public int getPriority() {
return priority;
}
@Override
public String toString() {
return "GiftType [name=" + name + ", quantity=" + quantity + ", priority=" + priority + "]";
}
}
// 奖品
class Gift {
// 每个奖品有唯一id,抽奖不会重复,格式为"16等奖——第8号"
private String id;
// 这个奖品的类别
private GiftType type;
public Gift(String id, GiftType type) {
thisid = id;
thistype = type;
}
public GiftType getType() {
return type;
}
@Override
public String toString() {
return "Gift [id=" + id + ", type=" + type + "]";
}
}
参考
int custNO;//客户会员号(说明:customer指客户)
Systemoutprintln ("请输入四位会员卡号:");//输入会员卡号
Scanner input = new Scanner(Systemin);
custNO = inputnextInt();
if (custNo > 1000){
//获得每位数字(a/b是除a%b是取余)
int gewei = custNO % 10 ; //分解获得个位数 1001 %10 ==1
int shiwei = custNO /10 %10 ; //分解获得十位数 //1234 /10 % 10 = 123 %10 =3 (1234 /10 = 1234 % 10 )
int baiwei = custNO /100 %10 ;//分解获得百位数
int qianwei = custNO /1000 ; //分解获得千位数
}
//计算数字之和
int sum = gewei + shiwei + baiwei + qianwei ;
Systemoutprintln ("会员卡号"+custNO +"各位之和:" + sum );
以上就是关于谁能帮我写一个java抽奖小程序全部的内容,包括:谁能帮我写一个java抽奖小程序、用java编写一个彩票抽奖程序、JAVA抽奖的算法等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)