Difference between revisions of "Guess my Number"
m (EDIT: Added link to boolean data type) |
(Accepted previous edits and fixed Problem area and solution area) |
||
Line 2: | Line 2: | ||
|Problem= Generate a program where the computer chooses a random number between 1 and 10 and the user is continuously asked to guess it until they are correct. Use the following guidelines in coming up with a solution: | |Problem= Generate a program where the computer chooses a random number between 1 and 10 and the user is continuously asked to guess it until they are correct. Use the following guidelines in coming up with a solution: | ||
− | * [[ | + | * [[Input/Output using JOptionPane|JOptionPane for input and output]] |
* [[Math Methods|Math.random()]] for generating the random number | * [[Math Methods|Math.random()]] for generating the random number | ||
* [[While Loops|While loop]] for continuously asking for a guess until right | * [[While Loops|While loop]] for continuously asking for a guess until right | ||
Line 12: | Line 12: | ||
[[Image:Wiki_loops03.jpg|center]]<BR> | [[Image:Wiki_loops03.jpg|center]]<BR> | ||
− | |Solution=To use Math.random() and JOptionPane you will need to include the following import statements. | + | |Solution=To use Math.random() and JOptionPane you will need to include the following import statements. Without these statements you will get errors when compiling your code. |
<pre> | <pre> | ||
import java.lang.Math; //needed for Math.random | import java.lang.Math; //needed for Math.random | ||
Line 50: | Line 50: | ||
} | } | ||
</pre> | </pre> | ||
− | To | + | To continuously ask the user for a guess till correct, we can put the previous code inside of a while loop. |
<pre> | <pre> | ||
//loop until correct changes to true | //loop until correct changes to true |
Revision as of 21:06, 9 April 2010
Back to the Program-A-Day homepage
ProblemGenerate a program where the computer chooses a random number between 1 and 10 and the user is continuously asked to guess it until they are correct. Use the following guidelines in coming up with a solution:
|
While and For Loops | |
---|---|---|
SolutionTo use Math.random() and JOptionPane you will need to include the following import statements. Without these statements you will get errors when compiling your code. import java.lang.Math; //needed for Math.random import javax.swing.JOptionPane; //needed for JOptionPane We are also going to need 3 variables; int randNum, String guess, Boolean correct. int randNum; //used to store the randNum we generate String guess; //used to store the guess boolean correct = false; //loop variable initialized to false meaning our guess is incorrect To generate a random number between 1 and 10 we will need to use the following code. //(int)(Math.random() * 10 generates a random number from 0 to 9 //to get a random number from 1 to 10 we need to add 1 randNum = (int)(Math.random() * 10) + 1; To get a guess from the user we will need to prompt them using JOptionPane then convert it to an int. We will assume valid input from the user. After we check the guess we will print out a message indicating if the user is correct. //get the guess from the user guess = JOptionPane.showInputDialog("Enter a guess between 1 and 10:"); //ensure that the user entered something if(guess != null) { //parse the guess into an integer and compare it to the randNum //then print out a corresponding message if(Integer.parseInt(guess) == randNum) { //set correct to true so that we can exit the while loop correct = true; JOptionPane.showMessageDialog(null, "You are correct", "", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "You are incorrect", "", JOptionPane.INFORMATION_MESSAGE); } } To continuously ask the user for a guess till correct, we can put the previous code inside of a while loop. //loop until correct changes to true while(correct == false) { //get the guess from the user guess = JOptionPane.showInputDialog("Enter a guess between 1 and 10:"); //ensure that the user entered something if(guess != null) { //parse the guess into an integer and compare it to the randNum //then print out a corresponding message if(Integer.parseInt(guess) == randNum) { //set correct to true so that we can exit the while loop correct = true; JOptionPane.showMessageDialog(null, "You are correct", "", JOptionPane.INFORMATION_MESSAGE); } else { JOptionPane.showMessageDialog(null, "You are incorrect", "", JOptionPane.INFORMATION_MESSAGE); } } } For the final solution code you can look under the code heading. | ||
Code |