Difference between revisions of "Guess my Number"
Line 12: | Line 12: | ||
[[Image:Wiki_loops03.jpg|center]]<BR> | [[Image:Wiki_loops03.jpg|center]]<BR> | ||
− | |Solution=To begin | + | |Solution=To begin you need to get references for Math.random and Scanner |
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
− | int randNum; // | + | import javax.swing.*; |
− | Scanner | + | import java.lang.Math; |
− | + | import java.util.Scanner; | |
+ | }} | ||
+ | |||
+ | |||
+ | You're going to need 4 variables to use for this problem | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
+ | int randNum; //store the random number to be guess | ||
+ | Scanner input = new Scanner(System.in) //this will get the guess of the user | ||
+ | int guess; //store the guess of the user | ||
+ | Boolean correct = false; //this will serve as loop condition until user get the right answer | ||
}} | }} | ||
To generate a random number between 1 and 10 we will need to use the following code. | To generate a random number between 1 and 10 we will need to use the following code. | ||
− | |||
− | |||
{{CodeBlock | {{CodeBlock | ||
|Code= | |Code= | ||
Line 32: | Line 40: | ||
− | + | Use the Scanner to get the input of the user. using the method hasNextInt() will check if the user entered a valid number. | |
− | + | {{CodeBlock | |
− | + | |Code= | |
− | + | System.out.print("Enter a guess between 1 and 10: "); //get the guess from the user | |
− | //ensure that the user entered | + | |
− | + | if(input.hasNextInt()) //ensure that the user entered a number | |
{ | { | ||
− | // | + | guess = input.nextInt(); //will stored the number entered by the user |
− | + | ||
− | if | + | if(guess == randNum) // to compare if the guessed number is equal to the generated number from Math.random() |
{ | { | ||
− | //set correct to true | + | correct = true; //set correct to true if its equal |
− | + | System.out.println("You are correct"); | |
− | + | ||
} | } | ||
else | else | ||
{ | { | ||
− | + | System.out.println("You are incorrect"); | |
} | } | ||
} | } | ||
− | + | else //program will enter this if entered input was not a number | |
− | + | { | |
− | + | System.out.println("Numbers Only"); | |
+ | input.next(); | ||
+ | } | ||
+ | }} | ||
+ | |||
+ | |||
+ | Since you do not know when to stop the loop, [[While Loops|While Loop]] is necessary for this. Use the boolean you created to know if the loop needs to stop, in this case if the user guessed the number. | ||
+ | {{CodeBlock | ||
+ | |Code= | ||
//loop until correct changes to true | //loop until correct changes to true | ||
while(correct == false) | while(correct == false) | ||
{ | { | ||
− | + | //codes | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
− | + | }} | |
+ | |||
For the final solution code you can look under the code heading. | For the final solution code you can look under the code heading. | ||
|SolutionCode=import java.lang.Math; //needed for Math.random | |SolutionCode=import java.lang.Math; //needed for Math.random | ||
− | import | + | import java.util.Scanner; |
public class GuessMyNumber | public class GuessMyNumber | ||
Line 88: | Line 87: | ||
{ | { | ||
int randNum; //used to store the randNum we generate | int randNum; //used to store the randNum we generate | ||
− | + | Scanner input = new Scanner(System.in); //used to get the guess | |
+ | int guess = 0; //use to store the guess | ||
boolean correct = false; //loop variable initialized to false meaning our guess is incorrect | boolean correct = false; //loop variable initialized to false meaning our guess is incorrect | ||
Line 94: | Line 94: | ||
//to get a random number from 1 to 10 we need to add 1 | //to get a random number from 1 to 10 we need to add 1 | ||
randNum = (int)(Math.random() * 10) + 1; | randNum = (int)(Math.random() * 10) + 1; | ||
− | + | ||
while(correct == false) | while(correct == false) | ||
{ | { | ||
//get the guess from the user | //get the guess from the user | ||
− | + | System.out.print("Enter a guess between 1 and 10: "); //get the guess from the user | |
− | + | ||
− | if( | + | if(input.hasNextInt()) //ensure that the user entered a number |
{ | { | ||
− | + | guess = input.nextInt(); //will stored the number entered by the user | |
− | + | ||
− | + | if(guess == randNum) // to compare if the guessed number is equal to the generated number from Math.random() | |
− | + | { | |
− | + | correct = true; //set correct to true if its equal | |
− | + | System.out.println("You are correct"); | |
− | + | } | |
− | + | else | |
− | + | { | |
− | + | System.out.println("You are incorrect"); | |
− | + | } | |
− | + | }//if | |
− | } | + | else //program will enter this if entered input was not a number |
− | } | + | { |
− | } | + | System.out.println("Numbers Only"); |
+ | input.next(); | ||
+ | }//else | ||
+ | }//while | ||
+ | }//main | ||
} | } | ||
}} | }} |
Revision as of 13:27, 3 December 2011
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 begin you need to get references for Math.random and Scanner import javax.swing.*; import java.lang.Math; import java.util.Scanner;
int randNum; //store the random number to be guess Scanner input = new Scanner(System.in) //this will get the guess of the user int guess; //store the guess of the user Boolean correct = false; //this will serve as loop condition until user get the right answer
//(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;
System.out.print("Enter a guess between 1 and 10: "); //get the guess from the user if(input.hasNextInt()) //ensure that the user entered a number { guess = input.nextInt(); //will stored the number entered by the user if(guess == randNum) // to compare if the guessed number is equal to the generated number from Math.random() { correct = true; //set correct to true if its equal System.out.println("You are correct"); } else { System.out.println("You are incorrect"); } } else //program will enter this if entered input was not a number { System.out.println("Numbers Only"); input.next(); }
//loop until correct changes to true while(correct == false) { //codes } For the final solution code you can look under the code heading. | ||
Code |