老虎机 : 3 个 0--9 之间数字(随机数),3 个数字完全一样一等奖;两个一样二等
奖;三个数相加后个位数是 5 为 3 等奖
随机数用int n = (int)(Math.random() * 100);
用(int)(Math.random() * 10)生成:3 个 0到9的随机数,再用if判断中的什么奖即可
你题目的解答代码如下:
public class Test
{
public static void main(String[] args)
{
int a,b,c;
a = (int)(Math.random()*10);
b = (int)(Math.random()*10);
c = (int)(Math.random()*10);
System.out.println("a:"+a+", b:"+b+", c:"+c);
if(a==b && a==c)
System.out.println("一等奖");
else if(a==b || a==c || b==c)
System.out.println("二等奖");
else if((a+b+c)%10==5)
System.out.println("三等奖");
else
System.out.println("没中奖");
}
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
int n = (int)(Math.random() * 900)+100;
根据上面表达式生成三位数,然后分别求出个、十、百位上的数。
再根据规则进行计算就行了。