4个简单java random 程序

1

public?class?Flip?{

public?static?void?main(String[]?args)?{?

int?heads?=?0;

int?tails?=?0;

for?(int?i?=?0;?i<100;?i++)?{

if?(Math.random()?<?0.5)?{

heads++;

}?else?{

tails++;

}

}

System.out.println("Heads?count:?"?+?heads);

System.out.println("Tails?count:?"?+?tails);

}

}

2

public?class?PairOfDice?{

private?int?die1;?

private?int?die2;?

private?int?diceNum;

public?static?void?main(String[]?args)?{

PairOfDice?dice;

//?1?die

dice?=?new?PairOfDice(1);

for?(int?i?=?0;?i<100;?i++){

dice.roll();

System.out.println(dice.getDie1());

}

//?2?dice

dice?=?new?PairOfDice(2);

for?(int?i?=?0;?i<100;?i++){

dice.roll();

System.out.println(dice.getTotal());

}

}

public?PairOfDice(int?diceNum)?{?

this.diceNum?=?diceNum;?

}

public?void?roll()?{

if?(diceNum?==?1)?{

die1?=?(int)(Math.random()*6)?+?1;

}?else?{

die1?=?(int)(Math.random()*6)?+?1;

die2?=?(int)(Math.random()*6)?+?1;

}

}

public?int?getDie1()?{

return?die1;

}

public?int?getDie2()?{

return?die2;

}

public?int?getTotal()?{

return?die1?+?die2;

}

}

后面不想写了,第三题先随机生成一个输入,让用户输入一个,设置一个猜的count,用户输入一次,count+1,然后对比输入的数字和生成的数字,返回相应的提示

第四题根据第一题发展一下