public static void main(String[] args){
System.out.println("请输入您的4位会员卡号:")
Scanner sc = new Scanner(System.in)
int number = sc.nextInt() //接收用户从控制台输入的会员卡号,并保存在会员卡号变量中
int a = number/1000//千位
int b = number%1000/100//百位
int c = number%100/10//十位
int d = number%10 //个位
if((a+b+c+d)>20){
System.out.println("恭喜中奖!您是幸运客户")
}else{
System.out.println("谢谢参与!")
}
}
}
最基础的 没有异常判断 无限循环输入什么东西
找了两组程序:代码一:
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("祝大家鸿运!")
this.go()
}
public void go()
{
while(true)
{
if(sg==true)
{
String s = " "
for(int j = 1j <= 7j++)
{
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("欢迎使用本抽奖机", JLabel.CENTER)
JLabel linput = new JLabel("幸运号码:")
JTextField input = new JTextField(20)
JButton btnStart = new JButton("开始")
JButton btnStop = new JButton("停止")
JPanel lpanel = new JPanel()
JPanel btnPanel = new JPanel()
public LuckyNumberInterface() {
super("抽奖机")
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)//设置窗口的显示大小
setLocation(400, 300)//设置窗口显示的位置
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//开关
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)
}
}
<!DOCTYPE html><html>
<head>
<title>js随机抽奖一二三等奖不重复的抽奖逻辑怎么设置</title>
<meta charset="UTF-8" />
<script>
//添加一个随机函数
Math.rand = function(min, max){
function subRand(min, max){
min = min ? min : 0
max = max ? max : 9
var result = 0
do{
result = Math.random().toString().substr(2, 1)
}while(!(result >= min && result <= max))
return result
}
function getBit(maxBit){
maxBit = maxBit ? maxBit : max.toString().length
var result = [],
count = 0
for(var i = 0 i < maxBit i++){
result.push(subRand())
if(i != 0 && result[i] == result[i - 1]){
count++
}
}
return maxBit - count
}
min = min ? min : 0
max = max ? max : 0
var result = '',
bit = getBit()
do{
result = ''
for(var i = 0 i < bit i++){
result = result + subRand()
}
result = parseInt(result)
}while(!(result >= min && result <= max))
return result
}
//console.log(Math.rand(0, 100))
//LuckDraw 抽奖类 参数一 奖池数组, 参数二 中奖数量 返回 中奖索引
function LuckDraw(pool, numberOfWinners){
var results = [],
//是否已经中奖
isExists = function(index){
for(var i = 0 i < results.length i++){
if(results[i] == index){
return true
}
}
return false
},
subLuckDraw = function(){
do{
result = Math.rand(0, pool.length - 1)
}while(isExists(result))
return result
}
for(var i = 0 i < numberOfWinners i++){
results.push(subLuckDraw())
}
return results
}
var pool = [
'关',
'张',
'赵',
'马',
'黄',
'曹老板'
]
winners = LuckDraw(pool, 3) //返回从奖池中 中奖的索引,假设123等奖都只有一名的话各取一个即可
//假设123等奖 一等奖 1名, 二等奖 2名, 三等奖 3名, 只需要调用这个函数第二个参数设置为总和 6 即可
//然后从结果中第一个索引为一等奖 23 索引为二等奖 , 456索引为 三等奖
console.log(winners)
</script>
</head>
<body>
<script>
for(var i = 0 i < winners.length i++){
document.write(pool[winners[i]] + ' 恭喜你中了' + (i + 1) + '等奖<br />')
}
</script>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)