【3.15】彩票中奖

[3.15]Game:lottery

import java.util.Scanner;
public class LotteryGame {
    public static void main(String args[]){
        Scanner input=new Scanner(System.in);
        System.out.println("Enter three-digit integer to guess:");
        int guess=input.nextInt();
        int lottery=(int)(Math.random()*1000);
        System.out.println("The lottery number is "+lottery);

        int lottery1=lottery/100;
        int lotterytemp=lottery%100;    //可以不创建temp,直接lottery2=lottery%100/10
        int lottery2=lotterytemp/10;
        int lottery3=lotterytemp%10;
        int guess1=guess/100;
        int guesstemp=guess%100;
        int guess2=guesstemp/10;
        int guess3=guesstemp%10;

        if(lottery==guess)
        {
            System.out.println("You get it! The award is $10,000");
        }
        else
            {
            if(lottery1==guess1)
            {
                if(lottery2==guess3 && lottery3==guess2)
                {
                    System.out.println("You get all digits right! The award is $3,000");
                }
                else
                    {
                        System.out.println("You got 1 digit right! The award is $1000");
                    }
            }
            else if(lottery2==guess2)
                {
                    if(lottery1==guess3 && lottery3==guess1)
                    {
                        System.out.println("You get all digits right! The award is $3,000");
                    }
                    else
                    {
                        System.out.println("You got 1 digit right! The award is $1000");
                    }
                }
            else if(lottery3==guess3)
                {
                    if(lottery1==guess2 && lottery2==guess1)
                    {
                        System.out.println("You get all digits right! The award is $3,000");
                    }
                    else
                    {
                        System.out.println("You got 1 digit right! The award is $1000");
                    }
            }
            else
            {
                System.out.println("Pity!");
            }
        }
    }
}
//这个做出来,有个前提是只有当对应位数相同时才行。
//只有当某一位对应的相等时才有可能获奖
//书后题实现见LotteryGame大概率版(项目LotteryGame+,类名LotteryGames)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值