|
rig
|
May 22 2007, 04:07 PM
Post #1
|
1a5a6a4
- Posts:
- 229
- Group:
- Administrators
- Member
- #32
- Joined:
- June 4, 2006
|
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"); } }
|