Welcome Guest [Log In] [Register]



Add Reply
Sample Guessing Game; Made by me
Topic Started: May 22 2007, 04:07 PM (54 Views)
rig
1a5a6a4
Administrators
Heres a sample guessing game I made. Its smart to use the loops so that way it keeps repeating the program.

Code:
 
/*
* Made by Rig - Real Igloo Gaming v0.1
*/

import cs1.Keyboard;
import java.util.Random;

public class Guessinggame
{
   public static void main()
   {
       int guess,answer,times;
       boolean exit;
       String exitstr;
       exitstr="yes";
       exit=false;
       Random gen = new Random();
       while(exit==false)
       {
           times=0;
           guess=0;
           answer=gen.nextInt(100);
           System.out.println("\nIm thinking of a number from 1 to 100...\n");
           while(guess!=answer)
           {
               times++;
               System.out.print("What is your guess? ");
               guess=Keyboard.readInt();
               System.out.println();
               if(guess<answer)
               {
                   System.out.println("Your guess was lower than the answer.\n");
               }
               if(guess>answer)
               {
                   System.out.println("Your guess was greater than the answer.\n");
               }
           }
           System.out.print("\n Your guess was correct! It took you "+times+" guesses.\n");
           System.out.print("\n Exit? ");
           exitstr=Keyboard.readString();
           if(!exitstr.equalsIgnoreCase("no"))
           {
               exit=true;
           }
       }
       System.out.println("\n\n Thanks for playing! \n \t Made by Real Igloo Gaming \n \t v 0.1");
   }
}
Posted Image
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Java · Next Topic »
Add Reply


Theme designed by Sith of Outline