# Simple Guess Number Game : Java Program Code To Guess The Secret Or Random Number

Guess Number is a game in which user needs to guess the number between 1 and 52 (hardcoded) if you want you can make changes into the code

Its a fun game check it out once

The game is computer randomly selects the SECRET number with in the defined range of numbers , here 1 to 52 , and prompts the user to guess the number .

In this program we ask the user to enter any number between 1 to 52 . So user puts up any random number with in the defined range.

If the user number is same as the SECRET number then the game stops ,otherwise,

If the user number is lesser than the SECRET number then ,

It will change the range of the number , that is now user has to select a number between user input number to 52 .

If the user input is greater than the SECRET number then ,

the range of the numbers to select an input from for user will be 1 to user input .

The game continues till the user guesses the correct number .

Here we make sure that the game starts again once it is finished . You can also make it a automated program , that is computer will set the SECRET number and then computer only will try to find it . For that you just need to use random method again and compare it with the target or secret number .

for example :

Suppose computer selects 10 as the secret number

so , user try to guess the secret number between 1 to 52 , let suppose he chooses 38 which is greater than the secret number than the computer prompts user to guess the number from the range 1 to 37

instead if user inputs here 5 in first attempt , then the range would become 6 to 52 .

And in the same way the game continues to be played and the range to select the number keeps on decreasing . There is no limit set to define a select number of attempts to the user .

what to program in java

1. Computer randomly selects one secret number ,prompts user to guess it correctly in minimum number of

attempts with in defined range here 1 to 52

2. User inputs any number with in defined range

3. If

input number is greater than the secret number than range reduces to 1 to INPUT NUMBER

else

range reduces to INPUT NUMBER to 52

4. Repeat steps from 2 again

Its a fun game check it out once

**java computer programming**The game is computer randomly selects the SECRET number with in the defined range of numbers , here 1 to 52 , and prompts the user to guess the number .

In this program we ask the user to enter any number between 1 to 52 . So user puts up any random number with in the defined range.

If the user number is same as the SECRET number then the game stops ,otherwise,

If the user number is lesser than the SECRET number then ,

It will change the range of the number , that is now user has to select a number between user input number to 52 .

If the user input is greater than the SECRET number then ,

the range of the numbers to select an input from for user will be 1 to user input .

**java computer programming**The game continues till the user guesses the correct number .

**java coders**Here we make sure that the game starts again once it is finished . You can also make it a automated program , that is computer will set the SECRET number and then computer only will try to find it . For that you just need to use random method again and compare it with the target or secret number .

for example :

**java programmes**Suppose computer selects 10 as the secret number

so , user try to guess the secret number between 1 to 52 , let suppose he chooses 38 which is greater than the secret number than the computer prompts user to guess the number from the range 1 to 37

instead if user inputs here 5 in first attempt , then the range would become 6 to 52 .

And in the same way the game continues to be played and the range to select the number keeps on decreasing . There is no limit set to define a select number of attempts to the user .

**Pseudo code :**what to program in java

1. Computer randomly selects one secret number ,prompts user to guess it correctly in minimum number of

attempts with in defined range here 1 to 52

2. User inputs any number with in defined range

3. If

input number is greater than the secret number than range reduces to 1 to INPUT NUMBER

else

range reduces to INPUT NUMBER to 52

4. Repeat steps from 2 again

**Demo :***Code :*

import java.util.Scanner;

/****************************************************** /

** Program: CardsGame.java**

** Author: Sonia Rizvi**

** Date: 17 June 2016

/******************************************************/

public class CardsGame {

/**

* @param args

*/

static int low=1,high=52;

public static void main(String[] args) {

// TODO Auto-generated method stub

gameBegins();

}

public static void gameBegins()

{

int target=0;

double i =Math.random();

if(i != 0)

target= (int) (i*53);

else

{

gameBegins();

System.out.println("Please enter a valid number");

}

System.out.println("-------------------------");

System.out.println("NEW GAME ");

System.out.println("-------------------------");

System.out.println("");

System.out.println("");

System.out.println("pick a number between 1-52! ");

while(true)

{

int input=userInput();

predictionResult(input, target);

}

}

public static int userInput()

{

Scanner in = new Scanner(System.in);

int s= in.nextInt();

System.out.println("user guesses "+s);

return s;

}

public static void predictionResult(int input , int target)

{

if(input < target)

{

low=input;

System.out.println(" Sorry,that is too low ");

low=low+1;

System.out.println("pick a number between "+low +"-"+high);

}

else if(input > target)

{

high=input;

System.out.println(" Sorry,that number is too high ");

high=high-1;

System.out.println("pick a number between "+low +"-"+high);

}

else if(input==target)

{

System.out.println("That is correct !");

playAgain();

}

}

public static void playAgain()

{

low=1;

high=52;

gameBegins();

}

}

Simple Guess Number Game : Java Program Code To Guess The Secret Or Random Number
Reviewed by Sonia Rizvi
on
12:56
Rating:

## No comments: