彩票中奖器 (递归运用)
2016-03-07 10:53
323 查看
彩票中奖
简化题目:输入三个数,若顺序和数字相同中10000元(美金),若只是顺序不同数字相同中3000,
若只有一个匹配位置的数字相同中1000,做出这个抽奖程序!
===============================================
简化题目:输入三个数,若顺序和数字相同中10000元(美金),若只是顺序不同数字相同中3000,
若只有一个匹配位置的数字相同中1000,做出这个抽奖程序!
===============================================
package com.aiqiongdiao; import java.util.Scanner; class Test{ /** * 3个随机数: * 顺序:10000 * 匹配:3000 * 一个中:1000 */ public static int one(int num,int temp){ if(temp==num){ return 1; } else{ return 0; } } public static int two(int a[],int temp,int i,int temp2){ if(temp==0){ //temp的边界 return two(a,temp2,i+1,temp2); //返回数值temp2 } if(i>2){ //数组的边界 return 0; } if(a[i]==temp%10){ return two(a,temp2,i+1,temp2)+1; //返回值+1 }else{ return two(a,temp/10,i,temp2); } } public static int three(int a[],int temp,int i){ if(i<0||temp==0){ return 0; } if(a[i]==temp%10){ return 1; } else{ return three(a,temp/10,i-1); //此处不能自减 } } public static void main(String[] args) { /** * 获取数据 */ Scanner input =new Scanner(System.in); System.out.print("请输入三个数:"); int a[]=new int[3]; a[0]=input.nextInt(); a[1]=input.nextInt(); a[2]=input.nextInt(); int num=a[0]*100+a[1]*10+a[2]; int temp = (int) ( 899 * Math.random() + 100); //随机的temp // int temp=123; int temp2=temp; System.out.println("随机数是:"+temp); /** * 获奖函数 */ if(one(num,temp)==1){ System.out.println("尼玛,都10000$了"); } if((two(a,temp,0,temp2))==3){ System.out.println("糟了,有3000$了"); }else{ if(three(a,temp,2)==1){ //要让中一个和中三个平行开,避免重复 System.out.println("惨了,有1000$了"); }else{ System.out.println("太好了,啥鸟都没有"); } } } }
相关文章推荐
- 浅谈web网站架构演变过程
- Spring
- ASP.NET MVC4框架揭秘 源代码下载
- User Experience Collection
- 程序实践系列之库源码
- java并发编程(4)--线程池的使用
- properties文件value换行处理方式
- Struts2中出现严重: Dispatcher initialization failed错误
- 認識新一代標準色域Rec.BT.2020
- Eclipse文本编码格式修改为UTF-8
- iOS开发中图片拉伸的三个方法介绍
- iOS 删除所有子View
- DOOR SHELL FSR FTR FVR FVM FRR ISUZU 1996
- Java队列Queue
- Linux折腾之旅之linux mint 17.2
- UIImage转NSData,NSData转UIImage
- 深入理解C++中的mutable关键字
- 年月日 生日 js插件
- 小总结
- SOAP调用Web Service