
正文
java模拟抽奖代码 java抽奖游戏
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA 模拟抽奖,输入4位号码,如果这个号码的百位数能够被3整除,你就可以再免费领取一张电影票
package com.java.test01;
import java.util.Scanner;
/**
* JAVA 模拟抽奖,输入4位号码,
* 如果这个号码的百位数能够被3整除,
* java模拟抽奖代码你就可以再免费领取一张电影票
* @author Administrator
*
*/
public class Chou {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//循环,模拟一直抽奖
while(true){
System.out.println("请输入4位号码java模拟抽奖代码:");
if (sc.hasNext()) {
//获取号码
int number = sc.nextInt();
//获取百位的数字
int bai = number / 100 % 10;
//判断是否符合条件
if (bai % 3 == 0) {//能被3整除
System.out.println("恭喜你领到java模拟抽奖代码了一张电影票java模拟抽奖代码!");
}else{
System.out.println("很遗憾,未中奖!");
}
}else{
System.out.println("请正确输入!");
}
}
}
}
相关问答
Q1: 能不能帮我写一个模拟抽奖机的JAVA代码 1到32的八个数字
代码如下java模拟抽奖代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Luck extends JFrame implements ActionListener{
JTextField tf = new JTextField();
JButton b1 = new JButton("开始");
JButton b2 = new JButton("停止");
boolean isGo = false;
public Luck(){
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("welcome you! ");
this.go();
}
public void go(){
while(true){
if(isGo == true){
String s = "";
for(int j = 1; j = 8;j++){
int i = (int)(Math.random() * 31) + 1;
if(i 10){
s = s + " 0" + i;
}else{
s = s + " " + i;
}
}
tf.setText(s);
}
try{
Thread.sleep(10);
}catch(java.lang.InterruptedException e){
e.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if(s.equals("start")){
isGo = true;
b1.setEnabled(false);
b2.setEnabled(true);
}else{
isGo = false;
b2.setEnabled(false);
b1.setEnabled(true);
}
}
public static void main(String[] args){
new Luck();
}
}
看下是java模拟抽奖代码你java模拟抽奖代码的要求不。若有疑问Hijava模拟抽奖代码我。
Q2: 通过java 实现 模拟抽奖
import java.util.Scanner;
public class Choujiang {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//定义Scanner类1234
System.out.println("请输入四位卡号:");//输入提示语
int cardNo = sc.nextInt();//接受输入数据
int gewei = cardNo%10;//分解个位
int shiwei = cardNo/10%10;//分解十位
int baiwei = cardNo/100%10;//分解百位
int qianwei = cardNo/1000;//分解千位
int sum = gewei+shiwei+baiwei+qianwei;
System.out.println("会员卡号四位之和是:"+sum);
if(sum20){//判断各位数和是否大于20
System.out.println("恭喜,你是幸运客户!");
}else{
System.out.println("谢谢惠顾!");
}
}
}
Q3: java抽奖程序
我给java模拟抽奖代码你个比较简单的,,但是需要按照java模拟抽奖代码你的要求进行稍微的修改。。然后在main方法中去执行就可以java模拟抽奖代码了:
public class GoodLuck {
int custNo;
int i=1;
String answer;
String awardName;
public void LuckNo(){
Scanner input=new Scanner(System.in);
System.out.println("\n我行我素购物管理系统 幸运抽奖\n");
do{
// 需要的话请把随机数调整成你想要的范围(我这个是为了测试方便写的1
(~3的随机数,根据你的需要把下面的3换成你想要的数字就行了)
int num=(int)(Math.random()*3+1);
System.out.print("请输入会员卡号(4位整数):");
custNo=input.nextInt();
//百位数与随机数相同的为幸运者
int bai=custNo/100%10;
while(i==1){
if(custNo=1000custNo=9999){
break;
}
else{
System.out.println("\n会员号码输入有误,请重新输入:");
custNo=input.nextInt();
continue;
}
}
if(bai==num){
showAward();
System.out.print("\n卡号:"+custNo+"是幸运客户,获得"+awardName);
}else{
System.out.print("\n卡号:"+custNo+"\t谢谢您的支持!");
}
System.out.println("\n是否继续(y/n)");
answer=input.next();
while(i==1){
if(answer.equals("y")||answer.equals("n")){
break;
}else{
System.out.print("输入有误!请重新输入:");
answer=input.next();
continue;
}
}
}while(!answer.equals("n"));
}
public void showAward(){
int num=(int)(Math.random()*3+1);
if(num==1){
awardName="Mp3";
}
else if(num==2){
awardName="美的微波炉";
}
else{
awardName="美的电饭锅";
}
}
Q4: Java代码实现抽奖:从班级的学号中抽出一个一等奖,两个二等奖,三个三等奖
抽取问题, 重点是 同一个学号不能重复被抽取.
解决办法很多,
比如数组可以使用下标来标记,号码是否被使用,使用了就继续下一次抽取
也可以使用集合来抽取,把集合顺序打乱,然后随便抽几个就可以了
参考代码:数组法
import java.util.Random;
public class Test {
public static void main(String[] args) {
int stuNums=30;
int[] nums=new int[stuNums];//存储学号的数组
boolean[] flags=new boolean[stuNums];//标记,用于标记对应下标的学号是否已经被抽取过了
for (int i = 0; i stuNums; i++) {
nums[i]=i+1;//给学号赋值
}
Random r=new Random();
while(true){
int index = r.nextInt(stuNums);
if(!flags[index]){
System.out.println("A等:"+nums[index]);
flags[index]=true; //标记已经被使用过了
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--;//如果已经被抽取过了 ,那么i建议,再次循环
}
}
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));
}
}
Q5: 商场推出幸运抽奖活动的java初级代码编写
public class Lucky {
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("谢谢参与!");
}
}
}
最基础的 没有异常判断 无限循环输入什么东西
关于java模拟抽奖代码和java抽奖游戏的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







