Thursday, June 11, 2009

Simple Guessing Games


this program is for simple guessing game

rule : Guess the computer generated number

in this game computer generate a number but we can't see it.
we have 7 times to guess the number
each time we get some marks
there are hint to guide you to answer

if you guess first time we give u 21 marks
for second time 18 marks

like that marks will be reduced

so enjoy it.....:-)




import java.util.Random;
import java.util.Scanner;
class Guess
{
public static void main (String [] args)
{
Random randomNo= new Random();
Scanner input= new Scanner(System.in);
int rand;
int exit;
boolean play=true; // Set play True becoz play must continue
boolean won=false;
int turn=0;
while (play==true)// check play continue or want to exit
{
System.out.print("IF YOU CAN..... (Guessing game{Try to guess the computer generated Number})");
rand=randomNo.nextInt(100);// Generate Random number
for (int i=1 ; i<=7 ; i++) { System.out.print("Enter Your Guess : "); int guessVal=input.nextInt();//Give chance to input user turn=i; if (guessVal==rand) // Check Guess value is equal to Random number { won=true; // if Guess is correct User is win break; } else if (guessValrand) // if random number less than guess number
System.out.println("Try a number less than "+guessVal);
}
if (won=true) // if user win calculate marks
{
int marks=0;
if (turn==1)
marks=21;
else if (turn==2)
marks=18;
else if (turn==3)
marks=15;
else if (turn==4)
marks=12;
else if (turn==5)
marks=9;
else if (turn==6)
marks=6;
else if (turn==7)
marks=3;
else if (turn==8)
marks=0;
System.out.println("Your mark is : " + marks);
System.out.print("If You want to Play Again Enter 1, Quit Enter 0 : ");
exit=input.nextInt(); // if user enter 1 play is continue otherwise stop the game
if (exit==0)
play=false;
}
else
{
System.out.println("Game is Over");
System.out.print("If You want to Play Again Enter 1, Quit Enter 0 : ");
exit=input.nextInt();// if user enter 1 play is continue otherwise stop the game
if (exit==0)
play=false;
}
}

}
}

No comments:

Post a Comment