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);
}
}
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);
}
}
相关文章推荐
- Rhino 是一个完全使用Java语言编写的开源JavaScript实现。Rhino通常用于在Java程序中,为最终用户提供脚本化能力。它被作为J2SE 6上的默认Java脚本化引擎。
- 用java做的一个彩票32选7的简单程序
- 一个java程序中涉及的内存区域
- Java_socket程序学习03-一个客户端对一个服务器-交互(服务器端)--01
- Java 实训1:编写一个窗体程序显示日历表。
- 如何利用记事本编写并运行一个java程序
- 一个简单的验证回文数java程序
- 在启动脚本中一般使用类似如下的命令执行一个Java程序
- 一个很好的 Java 开源网站:open-open
- 一个必看的Java程序
- java程序保护如何知识产权,特别提供一个java 开发的java 源代码级的混淆器
- 程序人生之一:一个四年 JAVA 程序员的工作经历
- 使用.bat文件运行Java程序的一个示例
- JAVA调用系统命令或可执行程序--返回一个Runtime运行时对象,然后启动另外一个进程来执行命令
- java控制台程序如何判断String字符串中只输入了一个回车
- 一个简单java程序的运行全过程
- 希望大家推荐一个javaweb开源框架
- 混淆器:java程序保护如何知识产权,特别提供一个java 开发的java 源代码级的混淆器
- 如何开发一个java开源框架-----Jvn框架之实现浏览器传参后台转成Bean(第四)
- 一个手机看北京交通监视器实时录像的开源程序