Best Projects

Bug Trackingsystem,Books online shopping,college management system,HR management system,Internet banking,Online chat room ,Online exam,Telephone billing system,Banking application,ATM database,airways reservation system,virtual network computing ,calculator,SMTP(simple mail transfer protocol)

Guessing Game

// Name: Guessing Game
// Description:Lets the user try to gues
// s a number from 1 to 10 in three tries.
// A total of 10 games may be played with a
// total of games won being given after the
// the game is over.
// By: Tom Fleming
//
// Returns:The game tells you if you are
// right or wrong, and if you are hot, warm
// or cold to the number being guessed.
//



import java.text.*;
import java.io.*;
class game
{
public static void main( String[] args ) throws IOException
{
BufferedReader stdin = new BufferedReader(new InputStreamReader( System.in ) );
int guess = 0, limit = 9, x = 0, n, a = 0 ;
double val = 0 ;
String inputData;
for ( x = 1; x <= 10; x++) //number of games played
{
System.out.println("round " + x + ":");
System.out.println(" ");
System.out.println("I am thinking of a number from 1 to 10. ");
System.out.println("You must guess what it is in three tries. ");
System.out.println("Enter a guess: ");
inputData = stdin.readLine();
guess = Integer.parseInt( inputData );
val = Math.random() * 10 % limit + 1; //max limit is set to 9
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(0); //format number of decimal places
String s = nf.format(val);
val = Integer.parseInt( s );
for ( n = 1; n <= 3; n++ ) //number of guess's made
{
if ( guess == val)
{
System.out.println("RIGHT!!"); //if guess is right
a = a + 1;
System.out.println("You have won " + a + " out of " + x + " rounds");
System.out.println(" ");
n = 3;
continue;
}
if (n == 3 && guess != val) //3 guesses and guess is wromg
{
System.out.println("wrong");
System.out.println("The correct number was " + val);
System.out.println("You have won " + a + " out of " + x + " rounds");
System.out.println(" ");
continue;
}
//how close guess is to value
if ( guess == val - 1 || val + 1 == guess) //Within 1
System.out.println("hot");
else if ( guess == val - 2 || val + 2 == guess) // Within 2
System.out.println("warm");
else
System.out.println("cold"); // Greater than 3
inputData = stdin.readLine();
guess = Integer.parseInt( inputData );
}
}
//ratings
if ( a <= 7)
System.out.println("Your rating is: imbecile.");
else if ( a <= 8)
System.out.println("Your rating is: getting better but, dumb.");
else if (a <= 9)
System.out.println("Your rating is: high school grad.");
else if ( a == 10)
System.out.println("Your rating is: College Grad.!!!");
}
}

No comments: