Today we made some changes to the Number Guessing Game:
// ======================
// Number Guessing Game
// Version 1.1
// By Robert Griffith
// ======================
import java.util.Scanner;
public class NumberGuessingGame2 {
public static void main(String[] args)
{
int secretNumber;
int guess;
int tries;
int maxtries;
secretNumber = (int) (Math.random() * 999 + 1);
//System.out.println("Secret number is " + secretNumber); // to be removed later
Scanner keyboard = new Scanner(System.in);
tries=0;
maxtries=10;
do {
tries=tries+1;
System.out.print("Enter guess #" + tries + ": ");
guess = keyboard.nextInt();
System.out.println("Your guess is " + guess);
if(tries > maxtries)
{
System.out.println("You have exceeded the maximum number of tries. The secret number was " + secretNumber + ". Sorry!");
System.exit(0);
}
if(guess == secretNumber)
{
System.out.println("Your guess is correct! It took you " + tries + " tries. Congratulations!");
}
if (guess < secretNumber)
{
System.out.println("Your guess is smaller than the secret number.");
}
if (guess > secretNumber)
{
System.out.println("Your guess is larger than the secret number.");
}
} while (guess != secretNumber);
}
}
Wednesday, September 11, 2013
Subscribe to:
Post Comments (Atom)
Blog Archive
-
▼
2013
(59)
-
▼
September
(26)
- Trick the Turtle: Adding Sound
- Trick the Turtle: Adding Control
- Trick the Turtle: Cleaning Up the Code and Adding ...
- Coding with Greenfoot: Trick the Turtle (Continued)
- Coding with Greenfoot: Trick the Turtle
- Beginning Greenfoot: Objects and Classes
- Case Study: Rock, Paper, Scissors
- Case Study: Using Strings (Haiku)
- Looking at Code: Alphabet Formatting
- Homework: Flow Chart & Begin Code for Rock, Paper,...
- My Flow Chart: Minimum Coins Challenge
- Flow Chart & Code Homework
- Some Interesting Video Clips
- ASCII Text Generator
- Flow Chart and Code: Coin Toss
- For Loops
- Number Guessing Game: Revisited
- JAVA Case Study #4: Number Guessing Game
- JAVA Case Study #3: Magic Cue Ball
- JAVA Case Study #1: Movie Tickets
- JAVA Case Study #2: Divide Items Equally
- Looking at Source Code & Flow Charts
- About Programming
- Flow Charts
- AP Computer Science
- Welcome to Week One of "AP Computer Science"! Alt...
-
▼
September
(26)
No comments:
Post a Comment