Here is the java code
import java.util.Scanner; public class GuessingGame { /** * This program presents a guessing game * where the computer randomly selects * a number between 0 and 1000 * and the player gets 10 chances to guess * the player can play as many times * as he or she wants and the program * will list total wins and average score */ int gameCounter=0; int[] scoreArray=new int[50]; public static void main(String[] args) { //instantiate the class GuessingGame gg = new GuessingGame(); //get the scanner Scanner reader = new Scanner(System.in); //initialize the variable for exiting int choice=1; //loop so the player can play until they choose //to quit do { //call the start of the game gg.Start(); //increment the number of games gg.gameCounter++; System.out.println("Do you want to play again? 0 to quit"); choice=reader.nextInt(); }while (choice != 0); } private void Start() { //Call the method to get the random number double num=GetRandom(); //the line below just for testing //System.out.println((int)num); //cast the random number to an integer //and pass to the GetGuess method GetGuess((int)num); } private double GetRandom() { //Get the random number double number = Math.random()*1000; return number; } private void GetGuess(int number) { int score; //declare score counter Scanner reader = new Scanner(System.in); //loop backwards to keep score for(score=10; score > 0; score--) { System.out.println("Enter a quess between 0 and 1000"); double guess=reader.nextDouble(); //check the guesses if(guess > number) System.out.println("Your guess is too high"); else if(guess < number) System.out.println("Your guess is too low"); else { System.out.println("congratulations"); break; //leave the loop if correct } } GetScores(score);//call the get scores method } private void GetScores(int score) { //output the current score System.out.println("Your current Score is " + score); //assign it to the scoreArray scoreArray[gameCounter]=score; //initialize the variable to get the totals int scoreTotal=0; //loop through the array to get the total for (int i=0;i<50;i++) { scoreTotal+=scoreArray[i]; } //get the average casting it to a double to get decimal //pars double scoreAverage=(double)scoreTotal/(double)(gameCounter + 1); //pring out the results System.out.println("Your have played " + (gameCounter + 1) + "games"); System.out.println("Your average score is " + scoreAverage); } }
Here is a transcript of the console when the program is running
Enter a quess between 0 and 1000 500 Your guess is too low Enter a quess between 0 and 1000 750 Your guess is too low Enter a quess between 0 and 1000 875 Your guess is too high Enter a quess between 0 and 1000 800 Your guess is too low Enter a quess between 0 and 1000 850 Your guess is too high Enter a quess between 0 and 1000 825 Your guess is too high Enter a quess between 0 and 1000 812 Your guess is too low Enter a quess between 0 and 1000 819 Your guess is too low Enter a quess between 0 and 1000 822 Your guess is too low Enter a quess between 0 and 1000 823 congratulations Your current Score is 1 Your have played 1games Your average score is 1.0 Do you want to play again? 0 to quit 1 Enter a quess between 0 and 1000 500 Your guess is too high Enter a quess between 0 and 1000 250 Your guess is too low Enter a quess between 0 and 1000 350 Your guess is too low Enter a quess between 0 and 1000 425 Your guess is too low Enter a quess between 0 and 1000 475 Your guess is too high Enter a quess between 0 and 1000 450 Your guess is too high Enter a quess between 0 and 1000 433 Your guess is too low Enter a quess between 0 and 1000 444 Your guess is too high Enter a quess between 0 and 1000 439 Your guess is too low Enter a quess between 0 and 1000 441 Your guess is too high Your current Score is 0 Your have played 2games Your average score is 0.5 Do you want to play again? 0 to quit 0
No comments:
Post a Comment