Difference between revisions of "Week 6"
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{1010PrAD | {{1010PrAD | ||
− | |ProblemName= | + | |ProblemName=Roulett |
− | |Problem=The | + | |Problem= Please read the following carefully, and Write a complete program Roulett that prints out<br/> |
+ | total gained from player.<br/> | ||
+ | <br/> | ||
+ | Each game will be running for 6 turns. Player will take the first turn <br/> | ||
+ | followed by the opponent. You need to generate a double random number <br/> | ||
+ | each time that will represent the chance of getting shot. <br/> | ||
+ | If the chance is smaller than the probability, then the bullet is fired.<br/> | ||
+ | First turn, the probability starts at 0.2. After the first turn, you have to increase the probability. <br/> | ||
+ | It will increase 0.1 for first four times, then, it will increase 0.25 for the 5th and the 6th time.<br/> | ||
+ | If the player wins the player gets $ 50 dollars, otherwise the player loses $ 50 dollars.<br/> | ||
+ | The starting money of the player will be $ 100 dollars.<br/> | ||
+ | <br/> | ||
+ | Example. 1st(player, 0.2), 2nd(opponent, 0.3), 3rd(player, 0.4), 4th(opponent, 0.5), 5th(player, 0.75), 6th(opponent, 1.0)<br/> | ||
+ | <br/> | ||
+ | Print out the output (money of the player) using JOPtionPane.<br/> | ||
− | |SolutionCode=public class | + | |SolutionCode=<pre>import java.lang.Math.*; |
+ | import javax.swing.*; | ||
+ | |||
+ | public class Roulett | ||
{ | { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | boolean win = false; | ||
+ | int money = 100; | ||
+ | |||
+ | for(int i = 0; i < 20; i++) | ||
+ | { | ||
+ | double probability = 0.2; | ||
+ | |||
+ | for(int j = 0; j < 6; j++) | ||
+ | { | ||
+ | double chance = Math.random(); | ||
+ | |||
+ | if(probability > chance) | ||
+ | { | ||
+ | if(j % 2 == 0) | ||
+ | { | ||
+ | money -= 50; | ||
+ | win = false; | ||
+ | } | ||
+ | |||
+ | else | ||
+ | { | ||
+ | money += 50; | ||
+ | win = true; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if(j < 4) | ||
+ | { | ||
+ | probability += 0.1; | ||
+ | } | ||
+ | |||
+ | else | ||
+ | { | ||
+ | probability += 0.25; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | JOptionPane.showMessageDialog(null, "Player's money left: " + money); | ||
+ | } | ||
} | } | ||
+ | </pre> | ||
|SideSectionTitle=...by students | |SideSectionTitle=...by students |
Latest revision as of 14:36, 5 April 2010
Back to the Program-A-Day homepage
ProblemPlease read the following carefully, and Write a complete program Roulett that prints out |
...by students
An image or By Students section | |
---|---|---|
SolutionThe solution... | ||
Code |