产生三位整数的彩票。程序提示用户输入一个三位整数,然后依照下面的规则判定用户是否赢得奖金:
1)如果用户输入的所有数匹配彩票的确切顺序,奖金是10 000美元。
2)如果用户输入的所有数匹配彩票的所有数字,奖金是3 000美元。
3)如果用户输入的其中一个数匹配彩票号码中的一个数,奖金是1 000美元。
package pack2;
import java.util.Scanner;
public class Lottery {
public static void main(String[] args) {
try(Scanner input = new Scanner(System.in);) {
System.out.print("Enter a three-digit integer as your guess for lottery: ");
System.out.println(guessLottery(input.nextInt()));
}
}
//彩票
public static String guessLottery(int guess) {
int value = 100+(int)(Math.random() * 900); //生成随机彩票号码
return "The lottery value is "+value+"\n"+compatible(value, guess);
}
//彩票匹配
public static String compatible(int value, int guess) {
int v1 = value % 10; //获取末位数
int v2 = value / 10 % 10; //获取中间位数
int v3 = value / 10 / 10 % 10; //获取首位数
int g1 = guess % 10;
int g2 = guess / 10 % 10;
int g3 = guess / 10 / 10 % 10;
if(value == guess) //处理情况1)
return "You will win $10 000";
else if((g1 == v1 || g1 == v2 || g1 == v3) && //处理情况2)
(g2 == v1 || g2 == v2 || g2 == v3) &&
(g3 == v1 || g3 == v2 || g3 == v3))
return "You will win $3 000";
else if(g1 == v1 || g1 == v2 || g1 == v3 || //处理情况3)
g2 == v1 || g2 == v2 || g2 == v3 ||
g3 == v1 || g3 == v2 || g3 == v3)
return "You will win $1 000";
else //处理全不匹配的情况
return "No prize";
}
}