
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>=1000&&custNo<=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="美的电饭锅"
}
}
用一个Int[]数组记录随机到的数字,插代码:
int[] count=new i[6]//用于接收生成的随机数
for(int i=0i<i.lengthi++)
{
Random rand = new Random()int c = rand.nextInt()//int范围类的随机数 c = rand.nextInt(30)//生成0-30以内的随机数 c = (int)(Math.random() * 30)//0-30以内的随机数 count[i]=c}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)