[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)