首页 > 试题广场 >

(游戏:彩票)修改程序清单 3-8,产生三位整数的彩票。程

[问答题]
 (游戏:彩票)修改程序清单 3-8,产生三位整数的彩票。程序提示用户输入一个三位整数,然 后依照下面的规则判定用户是否贏得奖金: 
1 )如果用户输人的所有数匹配彩票的确切顺序,奖金是丨 0 000美元。
 2) 如果用户输人的所有数匹配彩票的所有数字,奖金是 3 000 美元。
 3 )如果用户输人的其中一个数匹配彩票号码中的一个数,奖金是 1 000美元。 
public class Test {
	public static void main(String[] args){
		

		//程序说明:产生一个三位数,提示用户输入一个三位数然后确定奖金
		//随机产生一个三位数
		int s = (int)(Math.random() * 900 + 100);
		//提示用户输入
		System.out.print("Enter your lottery pick(three digits):");
		Scanner input = new Scanner(System.in);
		int y = input.nextInt();
		if(y < 100 || y > 999)
		{
			System.out.println("error input");
			return;
		}
		//确定奖金
		  //提取
		int s_ge = s % 10;
		int s_shi = s / 10 % 10;
		int s_bai = s /100;
		  //排序
		int temp = -1;
		if(s_bai > s_shi){
			temp = s_bai;
			s_bai = s_shi;
			s_shi = temp;
		}
		if(s_bai > s_ge){
			temp = s_bai;
			s_bai = s_shi;
			s_shi = temp;
		}
		if(s_shi > s_ge){
			temp = s_shi;
			s_shi = s_ge;
			s_ge = s_shi;
		}
			
		int y_ge = y % 10;
		int y_shi = y / 10 % 10;
		int y_bai = y /100;
		if(y_bai > y_shi){
			temp = y_bai;
			y_bai = y_shi;
			y_shi = temp;
		}
		if(y_bai > y_ge){
			temp = y_bai;
			y_bai = y_shi;
			y_shi = temp;
		}
		if(y_shi > y_ge){
			temp = y_shi;
			y_shi = y_ge;
			y_ge = y_shi;
		}
		if(s == y){
			System.out.println("Exact match:you win $10000.");
		}
		else if(s_ge == y_ge && s_shi == y_shi && s_bai == y_bai)
			 System.out.println("Match all digits:you win $3000");
		else if(s_ge == y_ge || s_ge == y_shi || s_ge == y_bai || s_shi == y_ge || s_shi == y_shi || s_shi == y_bai || s_bai == y_ge || s_bai == y_shi || s_bai == y_bai)
			System.out.println("match one digit:you win $1000.");
		else
			System.out.println("sorry : no match");
    }
}

发表于 2020-02-24 11:28:29 回复(0)