PDA

View Full Version : ChanceGameEndWhenGoalIsReached2:30


halfstar
02-23-04, 02:31 PM
Chance GAME confuse.....
I'm a first year student and I'm confuse about how to do this I don't know where to plug in the random number or where to use it........

"Goal : Hand-in your code with a goal of 30, but design the program so that the goal can be updated easily.



Random Numbers: Use this code to get the computer to generate a random int >=1 and <= 10:



r = (int) (Math.random () * 10)+1;



Input and Output: After each move, the program should output a line (using System.out.println) which says either:

1) "Good pick! You moved ahead " + x + " spaces. Currently you are at " + location.” or

2) "Oops! You moved back " + … + " spaces. Currently you are at " + location.” or

3) "Net result of nothing! You are still at " + location.”


************************************************** ***
*/
public class CGame
{

/**................................................ ..................main<br>
* PURPOSE: blah
* @param args is a list of command line arguments
*/
public static void main(String[] args)
{

JOptionPane.showMessageDialog(null, "Pick a number between 1 and\n 10. If you pick 1, you'll always move ahead. If you pick a higher\n number; you might move ahead faster,\n or you might move back! Your goal is to move\n ahead 30 spaces.");
String inputnum;
int num = 0;
int n = 0;
int r = 0; //=random number
int counter = 0;

do
{

r=(int)(Math.random() *10)+1;
}
while (num == 0);
{
inputnum =JOptionPane.showInputDialog("Enter an Integer");
num = Integer.parseInt(inputnum);

if((num >= 1) && (num <= 10))
{
System.out.println("Good Pick! you Moved ahead " + num +" spaces Currently you are at " + r);
counter ++;
}
//if ((num != 1) && (num >=1))
//{
// System.out.println("Oops! you moved back" + r +"Currently you are at" + num);
//}

//if (num == 30)
//{
// System.out.println("Congratulations! You moved ahead " + num+" spaces and you reached or exceeded your goal of 30 in " + r +" moves");
// JOptionPane.showConfirmDialog(null, "Do you want to play again?");
//}
}
System.out.println("integer " + num);
System.exit(0);
}

}