Difference between revisions of "Roulett"
Line 78: | Line 78: | ||
An image or By Students section | An image or By Students section | ||
− | |Solution=The | + | |Solution=First, analyize what you need to import. It tells you to generate random numbers.<br/> |
+ | You will need to import Math class which is in java.lang. Also, you will need swing <br/> | ||
+ | for printing out the money using JOptionPane.<br/> | ||
+ | <br/> | ||
+ | <pre> | ||
+ | import java.lang.*; | ||
+ | import javax.swing.*; | ||
+ | </pre> | ||
+ | <br/> | ||
+ | After imporing java.lang.Math.Random and javax.swing.JOptionPane, figure out which variables you will need.<br/> | ||
+ | <br/> | ||
+ | You will need the initial money which is 100 as integer.<br/> | ||
+ | <br/> | ||
+ | <pre> | ||
+ | int money is 100 | ||
+ | </pre> | ||
+ | <br/> | ||
+ | Then, you will need a for loop to run the games 20 times.<br/> | ||
+ | Inside the for loop, you will need to declare another variable for the probability.<br/> | ||
+ | The probability will start from 0.2, so set the probability variable equal to 0.2.<br/> | ||
+ | <br/> | ||
+ | <pre> | ||
+ | for(int i running 20 times, incremented by 1) | ||
+ | { | ||
+ | double probability is 0.2 | ||
+ | } | ||
+ | </pre> | ||
+ | <br/> | ||
+ | Once you set up the outer loop, you will need an inner loop for each game.<br/> | ||
+ | There will 6 turns for each game which means the loop will run 6 times.<br/> | ||
+ | <br/> | ||
+ | <pre> | ||
+ | for(int j running 6 times, incremented by 1) | ||
+ | { | ||
+ | } | ||
+ | </pre> | ||
+ | <br/> | ||
+ | What you need to do inside the inner loop, you will need to generate random numbers.<br/> | ||
+ | Random numbers can be generated by using<br/> | ||
+ | <br/> | ||
+ | <pre> | ||
+ | double probability = Math.random(); | ||
+ | </pre> | ||
+ | <br/> | ||
+ | The numbers are generated by Math.Random are double values.<br/> | ||
+ | You need double values for chances, so it's ok to leave it by itself.<br/> | ||
+ | However, if you want a random generator that generates integers between 1 - 99, <br/> | ||
+ | you will need to put: <br/> | ||
+ | <br/> | ||
+ | <pre> | ||
+ | int random = (int)(Math.random() * 99) | ||
+ | </pre> | ||
+ | <br/> | ||
+ | The reason for this is, Math.random() will generate numbers between 0.0 < x < 1.0.<br/> | ||
+ | You will need to multiply by 99 to make the generate numbers 0.0 < x < 99.0.<br/> | ||
+ | Remember, you must ''CAST'' the random numbers to int if you will assign the value to int.<br/> | ||
+ | <br/> | ||
+ | Now, once you generate the random numbers, you will need to use some conditions to check who won.<br/> | ||
+ | As it says on the question, if the given probability is greater than <br/> | ||
+ | the randomly generated double value, there was gun fire. <br/> | ||
+ | <br/> | ||
+ | You will also need to consider whose turn it is.<br/> | ||
+ | The player takes first turn, for example <br/> | ||
+ | 1st = Player, 2nd = Opponent, 3rd = player, 4th = Opponent, 5th = Player, 6th = Opponent. <br/> | ||
+ | We can find out whose turn it is by using mod (%). <br/> | ||
+ | If your loop starts from 0, then the player will take turns when the loop counter is even.<br/> | ||
+ | Here is the pseudo-code for the condition. | ||
+ | <br/> | ||
+ | <pre> | ||
+ | if(probability > chance) | ||
+ | { | ||
+ | if(j % 2 == 0) | ||
+ | money -= 50 | ||
+ | else | ||
+ | money += 50 | ||
+ | } | ||
+ | </pre> | ||
+ | This pseudo-code means if there was a gun fire, check whose turn it is.<br/> | ||
+ | Then if it was the player's turn, he loses money.<br/> | ||
+ | Otherwise, he wins money. <br/> | ||
+ | Note that money -= 50 is equal to:<br/> | ||
+ | <br/> | ||
+ | <pre> | ||
+ | money = money - 50; | ||
+ | </pre> | ||
+ | <br/> | ||
+ | After setting up the condition, we need to increase the probability.<br/> | ||
+ | If there was no gun fire, another person needs to take the next turn.<br/> | ||
+ | For the first four times, it will increase by 0.1.<br/> | ||
+ | For the fifth and the sixth time, it will increase by 0.25.<br/> | ||
+ | <br/> | ||
+ | <pre> | ||
+ | if(j < 4) | ||
+ | probability += 0.1 | ||
+ | else | ||
+ | probability += 0.25 | ||
+ | </pre> | ||
+ | <br/> | ||
+ | The code above is assuming that it starts from 0.<br/> | ||
+ | j < 4 will be 0, 1, 2 and 3 which is four times and increase the probability by 0.1.<br/> | ||
+ | Otherwise, by 0.25 by setting else.<br/> | ||
+ | <br/> | ||
+ | After increasing the probability, we are done with loops.<br/> | ||
+ | Now, the last step is printing out the money using simple JOptionPane method.<br/> | ||
+ | <br/> | ||
+ | <pre> | ||
+ | JOptionPane.showMessageDialog(null, money); | ||
+ | </pre> | ||
+ | <br/> | ||
+ | See below for the solution code. | ||
}} | }} |
Latest revision as of 20:14, 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 | |
---|---|---|
SolutionFirst, analyize what you need to import. It tells you to generate random numbers. import java.lang.*; import javax.swing.*;
int money is 100
for(int i running 20 times, incremented by 1) { double probability is 0.2 }
for(int j running 6 times, incremented by 1) { }
double probability = Math.random();
int random = (int)(Math.random() * 99)
if(probability > chance) { if(j % 2 == 0) money -= 50 else money += 50 } This pseudo-code means if there was a gun fire, check whose turn it is. money = money - 50;
if(j < 4) probability += 0.1 else probability += 0.25
JOptionPane.showMessageDialog(null, money);
| ||
Code |