public class Lucky {
創(chuàng)新互聯(lián)建站專注于湛江網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供湛江營(yíng)銷型網(wǎng)站建設(shè),湛江網(wǎng)站制作、湛江網(wǎng)頁(yè)設(shè)計(jì)、湛江網(wǎng)站官網(wǎng)定制、小程序設(shè)計(jì)服務(wù),打造湛江網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供湛江網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
public static void main(String[] args){
System.out.println("請(qǐng)輸入您的4位會(huì)員卡號(hào):");
Scanner sc = new Scanner(System.in);
int number = sc.nextInt(); //接收用戶從控制臺(tái)輸入的會(huì)員卡號(hào),并保存在會(huì)員卡號(hào)變量中
int a = number/1000; //千位
int b = number%1000/100; //百位
int c = number%100/10; //十位
int d = number%10; //個(gè)位
if((a+b+c+d)20){
System.out.println("恭喜中獎(jiǎng)!您是幸運(yùn)客戶");
}else{
System.out.println("謝謝參與!");
}
}
}
最基礎(chǔ)的 沒有異常判斷 無(wú)限循環(huán)輸入什么東西
抽取問(wèn)題, 重點(diǎn)是 同一個(gè)學(xué)號(hào)不能重復(fù)被抽取.
解決辦法很多,
比如數(shù)組可以使用下標(biāo)來(lái)標(biāo)記,號(hào)碼是否被使用,使用了就繼續(xù)下一次抽取
也可以使用集合來(lái)抽取,把集合順序打亂,然后隨便抽幾個(gè)就可以了
參考代碼:數(shù)組法
import?java.util.Random;
public?class?Test?{
public?static?void?main(String[]?args)?{
int?stuNums=30;
int[]?nums=new?int[stuNums];//存儲(chǔ)學(xué)號(hào)的數(shù)組
boolean[]?flags=new?boolean[stuNums];//標(biāo)記,用于標(biāo)記對(duì)應(yīng)下標(biāo)的學(xué)號(hào)是否已經(jīng)被抽取過(guò)了
for?(int?i?=?0;?i??stuNums;?i++)?{
nums[i]=i+1;//給學(xué)號(hào)賦值
}
Random?r=new?Random();
while(true){
int?index?=?r.nextInt(stuNums);
if(!flags[index]){
System.out.println("A等:"+nums[index]);
flags[index]=true;?//標(biāo)記已經(jīng)被使用過(guò)了
break;
}
}
for?(int?i?=?0;?i??2;?i++)?{
int?index?=?r.nextInt(stuNums);
if(!flags[index]){
System.out.println("B等:"+nums[index]);
flags[index]=true;
}else{
i--;//如果已經(jīng)被抽取過(guò)了?,那么i建議,再次循環(huán)
}
}
for?(int?i?=?0;?i??3;?i++)?{
int?index?=?r.nextInt(stuNums);
if(!flags[index]){
System.out.println("c等:"+nums[index]);
flags[index]=true;
}else{
i--;
}
}
}
}
集合法
import?java.util.ArrayList;
import?java.util.Collections;
public?class?Test2?{
public?static?void?main(String[]?args)?{
int?stuNums=20;
ArrayListInteger?list=new?ArrayListInteger();
for?(int?i?=?0;?i??stuNums;?i++)?{
list.add(i+1);
}
System.out.println("有序"+list);
Collections.shuffle(list);//打亂順序
System.out.println("亂序"+list);
System.out.println("A等"+list.get(0));
System.out.println("B等"+list.get(1));
System.out.println("B等"+list.get(2));
System.out.println("C等"+list.get(3));
System.out.println("C等"+list.get(4));
System.out.println("C等"+list.get(5));
}
}
參考
int custNO;//客戶會(huì)員號(hào)(說(shuō)明:customer指客戶)
System.out.println ("請(qǐng)輸入四位會(huì)員卡號(hào):");//輸入會(huì)員卡號(hào)
Scanner input = new Scanner(System.in);
custNO = input.nextInt();
if (custNo 1000){
//獲得每位數(shù)字(a/b是除a%b是取余)
int gewei = custNO % 10 ; //分解獲得個(gè)位數(shù) 1001 %10 ==1
int shiwei = custNO /10 %10 ; //分解獲得十位數(shù) //1234 /10 % 10 = 123 %10 =3 (1234 /10 = 123.4 % 10 )
int baiwei = custNO /100 %10 ;//分解獲得百位數(shù)
int qianwei = custNO /1000 ; //分解獲得千位數(shù)
}
//計(jì)算數(shù)字之和
int sum = gewei + shiwei + baiwei + qianwei ;
System.out.println ("會(huì)員卡號(hào)"+custNO +"各位之和:" + sum );
生成100個(gè)對(duì)象,對(duì)象有個(gè)屬性,其中10個(gè)是大獎(jiǎng),40個(gè)是小獎(jiǎng),50個(gè)是無(wú)獎(jiǎng)。
放到一個(gè)List里。
每次抽中的步驟
1、隨機(jī)生成0-List長(zhǎng)度之間的數(shù)值 ,去取List中的相應(yīng)對(duì)象,并移除這個(gè)對(duì)象。
代碼如下。:
獎(jiǎng)品對(duì)象類:
public class PrizeBean {
private String type;
public String getType() {
return eggType;
}
public void setType(String eggType) {
this.eggType = eggType;
}
}
獎(jiǎng)品池初始化代碼段:
{
List prizebeanList = new ArrayList();
for (int i = 0; i 10; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBean.setType(“大獎(jiǎng)“);
prizebeanList.add(prizeBean);
}
for (int i = 0; i 40; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBean.setType(“小獎(jiǎng)“);
prizebeanList.add(prizeBean);
}
for (int i = 0; i 50; i++) {
PrizeBean prizeBean = new PrizeBean();
prizeBean.setType(“無(wú)獎(jiǎng)“);
prizebeanList.add(prizeBean);
}
}
抽獎(jiǎng)代碼段:
/**
*獎(jiǎng)品池已經(jīng)空的,肯定返回?zé)o獎(jiǎng)了。。。
**/
if(prizebeanList.size()==0){
- 沒有中獎(jiǎng)哦,下次加油!
return;
}
/**
* 隨機(jī)生成,獎(jiǎng)品池中獎(jiǎng)品數(shù)量的數(shù)字。。取出獎(jiǎng)品池中的數(shù)字。。移除記錄。返回。。
*/
int resultnum = (int) (Math.random() * prizebeanList.size());
PrizeBean resultPrizeBean = prizebeanList.get(resultnum);
prizebeanList.remove(resultPrizeBean);
if(resultPrizeBean.getType() .eqauls("大獎(jiǎng)"){
- 恭喜,大獎(jiǎng)!
}else if(resultPrizeBean.getType() .eqauls("小獎(jiǎng)"){
- 運(yùn)氣不錯(cuò)哦,小獎(jiǎng)!
}else{
- 沒有中獎(jiǎng)哦,下次加油!
}.
找了兩組程序:
代碼一:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GoodLucky extends JFrame implements ActionListener
{
JTextField tf = new JTextField();
JButton b1 = new JButton("開始");
JButton b2 = new JButton("停止");
boolean sg=false;
public GoodLucky(){
b1.setActionCommand("start");
JPanel p = new JPanel();
p.add(b1);
p.add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
b2.setEnabled(false);
this.getContentPane().add(tf,"North");
this.getContentPane().add(p,"South");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,200);
this.setLocation(300,300);
Cursor cu = new Cursor(Cursor.HAND_CURSOR);
this.setCursor(cu);
this.setVisible(true);
tf.setText("祝大家鴻運(yùn)!");
this.go();
}
public void go()
{
while(true)
{
if(sg==true)
{
String s = " ";
for(int j = 1; j = 7;j++)
{
int i = (int)(Math.random() * 9) + 1;
if(i 10)
{
s = s + " 0" + i;
}
else
{
s = s + " " + i;
}
}
tf.setText(s);
}
try
{
Thread.sleep(100);
}
catch(java.lang.InterruptedException e)
{
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("start"))
{
sg = true;
b1.setEnabled(false);
b2.setEnabled(true);
}
else
{
sg= false;
b2.setEnabled(false);
b1.setEnabled(true);
}
}
public static void main(String[] args)
{
new GoodLucky();
}
}
代碼二:
package com.softeem.lesson03;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class LuckyNumberInterface extends JFrame {
JLabel ltitle = new JLabel("歡迎使用本抽獎(jiǎng)機(jī)", JLabel.CENTER);
JLabel linput = new JLabel("幸運(yùn)號(hào)碼:");
JTextField input = new JTextField(20);
JButton btnStart = new JButton("開始");
JButton btnStop = new JButton("停止");
JPanel lpanel = new JPanel();
JPanel btnPanel = new JPanel();
public LuckyNumberInterface() {
super("抽獎(jiǎng)機(jī)");
final LuckyNumber ln = new LuckyNumber(input);
ln.start();
Container container = getContentPane();
lpanel.add(linput);
lpanel.add(input);
btnPanel.add(btnStart);
btnPanel.add(btnStop);
Font font = new Font("宋體", Font.BOLD, 16);
ltitle.setFont(font);
container.add(ltitle, BorderLayout.NORTH);
container.add(lpanel, BorderLayout.CENTER);
container.add(btnPanel, BorderLayout.SOUTH);
btnStart.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ln.setFlag(true);
}
});
btnStop.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
ln.setFlag(false);
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 120);//設(shè)置窗口的顯示大小
setLocation(400, 300);//設(shè)置窗口顯示的位置
setVisible(true);//顯示窗口
}
public static void main(String[] args) {
new LuckyNumberInterface();
}
}
package com.softeem.lesson03;
import java.text.DecimalFormat;
import java.util.Random;
import javax.swing.JTextField;
public class LuckyNumber extends Thread {
private String luckyNumber = "";
private boolean flag = false;//開關(guān)
private JTextField input;
public LuckyNumber(JTextField input) {
this.input = input;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public void run() {
while (true) {
if (flag) {
generatorLuckyNumber();
}
}
}
public void generatorLuckyNumber() {
String[] firstNumber = { "130", "131", "134", "137", "135", "139",
"150", "151", "155", "158", "159" };
Random rd = new Random();
int number = rd.nextInt(10000);
DecimalFormat df = new DecimalFormat("0000");
String lastNumber = df.format(number);
luckyNumber = firstNumber[rd.nextInt(firstNumber.length)] + "****"
+ lastNumber;
input.setText("");
input.setText(luckyNumber);
}
}
用隨機(jī)數(shù)不就行了
int number = (int)(Math. random() * 3) + 1;
然后后面用switch就可以了