All pastes #2054125 Raw Edit

Wendy

public text v1 · immutable
#2054125 ·published 2011-05-05 07:40 UTC
rendered paste body
// Wendy Dang, CSE 142, Spring 2011, Section AC
// Programming Assignment #5 , 05/04/11

//This program plays a number guessing game.
//Overall results are recorded at the end. 

import java.util.*;

public class GuessingGame {
	public static final int MAXNUMBER = 101;
	
	public static void main(String[] args) {
		String answer = "y";
		
		giveHaiku();
		Scanner console = new Scanner(System.in);
		
		//Picks a random number from 1 to the constant inclusive
		Random rand = new Random(); 
		int number = rand.nextInt(MAXNUMBER);
		
		int gamesPlayed = 1;

			while (answer.equals("y") || answer.equals("Y")) {
				runGame(console, number); 
				playAgain(console);
				//check if user wants to play again
			} //->otherwise run overall score 
			
			
			
 		}
		//method to run a game
		public static int runGame(Scanner console, int number) {
			int numGuesses = 1;
			System.out.println("I'm thinking of a number between 1 and " + MAXNUMBER + "...");
			System.out.print("Your guess? ");
			int guess = console.nextInt();
				while (guess != number) {
					if (guess < number) {
						System.out.println("It's higher.");
					} else {
						System.out.println("It's lower.");
						}
				}//closes while
			System.out.print("Your guess? ");
			guess = console.nextInt();
			numGuesses++;
			System.out.println("You guessed it in " + numGuesses + " guesses!");

	}//closes method
	
	//method to play again
		public static String playAgain(Scanner console) {
			System.out.print("Play again? ");
			String answer = console.next();
			answer = (String)answer.charAt(0); //<-this part will evaluate to 'y'
		}	
	
// 		System.out.print("Play again? ");
// 		String reply = playAgain(console);

// 		gamesPlayed++;
			
		

// 			
// 		}
		//Method for printing final scores
		//public static void finalScores(
			//System.out.println("Your overall results: ");
			//System.out.println("Total games	= " + gamesPlayed);
			//System.out.println("Total guesses	= " + numGuesses);
			//double avgGuesses = numGuesses/gamesPlayed;
			//System.out.printf("Guesses/game   = %.1f\n", avgGuesses);
			//System.out.print("Best game       = ");
			//
			//
			
				
	
	//The intro haiku
			public static void giveHaiku() {
		System.out.println("An old silent pond,");
		System.out.println("A frog jumps into the pond,");
		System.out.println("Splash! Silence again.");
	}

	
}