彩票中奖器 (递归运用)

彩票中奖


简化题目:输入三个数,若顺序和数字相同中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("太好了,啥鸟都没有");
		}		}
	}
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值