/* We all have played Guess the Number in our childhood
* It is a two player game , in which one player has to guess the number ,the other player had selected .
* Although , he gets a limited amount of chances to do so.
* So lets begin :
*/
import java.util.*; //I am using Scanner class to take the input
class Guess_the_number
{
int num,ch ;//My global variables
Scanner in = new Scanner(System.in);
public void main()// The main method
{
System.out.println("Guess The Number");
Random ran = new Random();
for(int i = 0; ; )
{
System.out.println("\nTo guess a number between 0 and 100 , enter 0 \nTo guess a number between 0 and 1000 ,enter 1\nIf you want to exit ,enter any other number.");
int no_ch = in.nextInt();//To take user's choice
if(no_ch==0)
{
ch = 5 ;//Number of chances left
System.out.println("You will have 5 chances to guess the correct number \n");
num = 0 + ran.nextInt(100);//To get the random number
}
else if(no_ch==1)
{
ch=7;//Number of chances left
System.out.println("You will have 7 chances to guess the correct number \n");
num = 0 + ran.nextInt(1000);//To get the random number or computer's choice
}
else
{
System.out.println("Thank you for playing!!");
break;
}
while(ch!=0){
System.out.println("\nEnter your Guess");
int guess = in.nextInt();//To input the guess
boolean result = play(guess);
if(result==true)
break;
}
if(ch==0)
System.out.println("Hard Luck ! The number was "+num);
}
}
private boolean play(int guess)//The main computation
{
if(guess>num)
{
System.out.println("Lower!");
ch--;
System.out.println("You have "+ch+" chances left");
return false;
}
else if(guess<num)
{
System.out.println("Higher!");
ch--;
System.out.println("You have "+ch+" chances left");
return false;
}
else // If user's guess and computer's guess are equal
{
System.out.println("Good job! You did manage to guess the correct Number");
return true;
}
}
}
-Shashwat Srivastava
* It is a two player game , in which one player has to guess the number ,the other player had selected .
* Although , he gets a limited amount of chances to do so.
* So lets begin :
*/
My Output |
import java.util.*; //I am using Scanner class to take the input
class Guess_the_number
{
int num,ch ;//My global variables
Scanner in = new Scanner(System.in);
public void main()// The main method
{
System.out.println("Guess The Number");
Random ran = new Random();
for(int i = 0; ; )
{
System.out.println("\nTo guess a number between 0 and 100 , enter 0 \nTo guess a number between 0 and 1000 ,enter 1\nIf you want to exit ,enter any other number.");
int no_ch = in.nextInt();//To take user's choice
if(no_ch==0)
{
ch = 5 ;//Number of chances left
System.out.println("You will have 5 chances to guess the correct number \n");
num = 0 + ran.nextInt(100);//To get the random number
}
else if(no_ch==1)
{
ch=7;//Number of chances left
System.out.println("You will have 7 chances to guess the correct number \n");
num = 0 + ran.nextInt(1000);//To get the random number or computer's choice
}
else
{
System.out.println("Thank you for playing!!");
break;
}
while(ch!=0){
System.out.println("\nEnter your Guess");
int guess = in.nextInt();//To input the guess
boolean result = play(guess);
if(result==true)
break;
}
if(ch==0)
System.out.println("Hard Luck ! The number was "+num);
}
}
private boolean play(int guess)//The main computation
{
if(guess>num)
{
System.out.println("Lower!");
ch--;
System.out.println("You have "+ch+" chances left");
return false;
}
else if(guess<num)
{
System.out.println("Higher!");
ch--;
System.out.println("You have "+ch+" chances left");
return false;
}
else // If user's guess and computer's guess are equal
{
System.out.println("Good job! You did manage to guess the correct Number");
return true;
}
}
}
-Shashwat Srivastava
No comments:
Post a Comment