您的位置:首页 > 编程语言 > Java开发

java的一个彩票程序 开源之

2010-07-04 22:44 375 查看
import java.util.Random;

import java.util.Scanner;

public class lottery {

static int one,two,three,four,five,six,specical;

static String UserNum="";

static int DrawNum;

public static String CreatLuckyNum(){

Random rand=new Random();

String LuckyNum = "";

for(int i=0;i<7;i++){

LuckyNum +=(Integer.toString(rand.nextInt(10)));

}

// System.out.println(LuckyNum);

return LuckyNum;

}

public static void GetUserNum(){

System.out.println("please input your 7 numbers: ");

Scanner sca=new Scanner(System.in);

int x;

if(sca.hasNextInt()){

x=sca.nextInt();}

else{

System.out.println("输入的不是数字!!!");

x=0;

}

String str=Integer.toString(x);

if(str.length()==7)UserNum=str;

else{

str="";

System.out.println("可能输入长度不是7位,请重新输入!");

GetUserNum();

}

// System.out.println(UserNum);

}

public static void GetDrawNum(){

System.out.println("输入抽奖次数:");

Scanner ss=new Scanner(System.in);

if(ss.hasNextInt()){

DrawNum=ss.nextInt();

}

else{

System.out.println("注意!输入数字!!!");

GetDrawNum();

}

}

public static void FinalResult(){

GetUserNum();

GetDrawNum();

for(int i=0;i<DrawNum;i++){

String LuckyNum=CreatLuckyNum();

int howmuch=IsWin(LuckyNum,UserNum);

switch (howmuch) {

case 7:

specical++;

break;

case 6:

one++;

break;

case 5:

two++;

break;

case 4:

three++;

break;

case 3:

four++;

break;

case 2:

five++;

break;

case 1:

six++;

break;

default:

break;

}}

String str="恭喜你~~~";

if(specical>0)

str+="你获得特等奖"+Integer.toString(specical)+"次,";

if(one>0)

str+="你获得一等奖"+Integer.toString(one)+"次,";

if(two>0)

str+="你获得二等奖"+Integer.toString(two)+"次,";

if(three>0)

str+="你获得三等奖"+Integer.toString(three)+"次,";

if(four>0)

str+="你获得四等奖"+Integer.toString(four)+"次,";

if(five>0)

str+="你获得五等奖"+Integer.toString(five)+"次,";

if(six>0)

str+="你获得六等奖"+Integer.toString(six)+"次!";

else {

str="抱歉,你没有中奖!";

}

System.out.println(str);

}

public static int IsWin(String UserNum,String LuckyNum){

char x[]=UserNum.toCharArray();

char y[]=LuckyNum.toCharArray();

int howmuch = 0;

for(int i=0;i<x.length;i++){

if(x[i]==y[i])

howmuch++;

}

return howmuch;

}

/**

* @param args

*/

public static void main(String[] args) {

FinalResult();

System.exit(0);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: