Difference between revisions of "Guess my Number"

From CompSciWiki
Jump to: navigation, search
 
(2 intermediate revisions by the same user not shown)
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]]
+
* [[Input using Scanner|Scanner]] to use for input
 
* [[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 begin (after importing javax.swing.*) we're going to need 3 variables; int randNum, String guess, Boolean correct.
+
|Solution=To begin you need to get references for Math.random and Scanner
<pre>
+
{{CodeBlock
int randNum; //used to store the randNum we generate
+
|Code=
String guess; //used to store the guess
+
import javax.swing.*;
boolean correct = false; //loop variable initialized to false meaning our guess is incorrect
+
import java.lang.Math;
</pre>
+
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.
<br>
+
{{CodeBlock
Note: Math methods do not require an import statement. <br>
+
|Code=
<pre>
+
 
//(int)(Math.random() * 10 generates a random number from 0 to 9
 
//(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
 
//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;
</pre>
+
}}
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.
+
 
<pre>
+
 
//get the guess from the user
+
Use the Scanner to get the input of the user. using the method hasNextInt() will check if the user entered a valid number.  
guess = JOptionPane.showInputDialog("Enter a guess between 1 and 10:");
+
{{CodeBlock
//ensure that the user entered something
+
|Code=
if(guess != null)
+
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
 
{
 
{
     //parse the guess into an integer and compare it to the randNum
+
     guess = input.nextInt();  //will stored the number entered by the user
    //then print out a corresponding message
+
 
     if(Integer.parseInt(guess) == randNum)
+
     if(guess == randNum) // to compare if the guessed number is equal to the generated number from Math.random()
 
     {
 
     {
         //set correct to true so that we can exit the while loop
+
         correct = true;  //set correct to true if its equal
         correct = true;
+
         System.out.println("You are correct");
        JOptionPane.showMessageDialog(null, "You are correct", "", JOptionPane.INFORMATION_MESSAGE);
+
 
     }
 
     }
 
     else
 
     else
 
     {
 
     {
         JOptionPane.showMessageDialog(null, "You are incorrect", "", JOptionPane.INFORMATION_MESSAGE);
+
         System.out.println("You are incorrect");
 
     }
 
     }
 
}
 
}
</pre>
+
else //program will enter this if entered input was not a number
To continuously ask the user for a guess till correct, we can put the previous code inside of a while loop.
+
{
<pre>
+
    System.out.println("Numbers Only");
 +
    input.next();     
 +
}
 +
}}
 +
 
 +
{{OutputBlock
 +
|Code=
 +
Enter a guess between 1 and 10: 3
 +
You are incorrect
 +
Enter a guess between 1 and 10: a
 +
Numbers Only
 +
Enter a guess between 1 and 10: 1
 +
You are correct
 +
}}
 +
 
 +
 
 +
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)
 
{
 
{
    //get the guess from the user
+
  //codes
    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);
+
        }
+
    }
+
 
}
 
}
</pre>
+
}}
 +
 
 
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 javax.swing.JOptionPane; //needed for JOptionPane
+
import java.util.Scanner;
  
 
public class GuessMyNumber
 
public class GuessMyNumber
Line 82: Line 97:
 
     {
 
     {
 
         int randNum; //used to store the randNum we generate
 
         int randNum; //used to store the randNum we generate
         String guess; //used to store the guess
+
         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 88: Line 104:
 
         //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
             guess = JOptionPane.showInputDialog("Enter a guess between 1 and 10:");
+
             System.out.print("Enter a guess between 1 and 10: "); //get the guess from the user
            //ensure that the user entered something
+
 
             if(guess != null)
+
             if(input.hasNextInt()) //ensure that the user entered a number
 
             {
 
             {
                //parse the guess into an integer and compare it to the randNum
+
                  guess = input.nextInt();  //will stored the number entered by the user
                //then print out a corresponding message
+
 
                if(Integer.parseInt(guess) == randNum)
+
                  if(guess == randNum) // to compare if the guessed number is equal to the generated number from Math.random()
                {
+
                  {
                    //set correct to true so that we can exit the while loop
+
                      correct = true;  //set correct to true if its equal
                    correct = true;
+
                      System.out.println("You are correct");
                    JOptionPane.showMessageDialog(null, "You are correct", "", JOptionPane.INFORMATION_MESSAGE);
+
                  }
                }
+
                  else
                else
+
                  {
                {
+
                      System.out.println("You are incorrect");
                    JOptionPane.showMessageDialog(null, "You are incorrect", "", JOptionPane.INFORMATION_MESSAGE);
+
                  }
                }
+
            }//if
             }
+
            else //program will enter this if entered input was not a number
         }
+
            {
     }
+
                  System.out.println("Numbers Only");
 +
                  input.next();    
 +
             }//else
 +
         }//while
 +
     }//main
 
}
 
}
 
}}
 
}}

Latest revision as of 13:52, 3 December 2011

Back to the Program-A-Day homepage

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:

  • Scanner to use for input
  • Math.random() for generating the random number
  • While loop for continuously asking for a guess until right
  • Boolean variable for indicating when the while loop is done
 

While and For Loops

Wiki loops03.jpg

Solution

To begin you need to get references for Math.random and Scanner

 import javax.swing.*;
import java.lang.Math;
import java.util.Scanner; 


You're going to need 4 variables to use for this problem

 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.

 //(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; 


Use the Scanner to get the input of the user. using the method hasNextInt() will check if the user entered a valid number.

 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();      
} 
 Enter a guess between 1 and 10: 3
You are incorrect
Enter a guess between 1 and 10: a
Numbers Only
Enter a guess between 1 and 10: 1
You are correct 


Since you do not know when to stop the loop, 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.

 //loop until correct changes to true
while(correct == false)
{
   //codes
} 

For the final solution code you can look under the code heading.

Code

Solution Code

Back to the Program-A-Day homepage