Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Michelle Liang
- Question 3 (user tries to guess a random number generated by the computer) Pseudocode:
- 1. Create variables for the amount of guesses per game, the total amount of guesses from all games played, the amount of games played, the random number that the computer generates, and a scanner to ask the user for their guess. Tell the user what the random number generated is in between (in this case, 1-15), and ask the user to guess that number.
- 2. If the user's guess is too low, print out "too low" and ask the user if he/she wants to guess again. If too high, print out "too high" and ask the user if he/she wants to guess again. If the user's guess equals the random number, print out a congratulations, with the amount of guesses needed to guess the number during that game.
- 3. Then, ask the user if they want to play another game. If the user says yes, then reset the guess counter and play the game again. If the user declines, print out a summary of how many games the user played, how many guesses they made in total from all games played, and the average amount of guesses that the user made per game.
- Question 3 Code:
- //this is Michelle's code
- //11/9/16
- import java.util.*;
- public class question3
- {
- public static void main (String [] args)
- {
- int guess=0; //variable that counts number of guesses per game
- double plays=0; //variable that counts number of games played; double beceause used for the calculation of the average which is double
- double totalguess=0; //variable that counts grand total of guesses from all games combined; double beceause used for the calculation of the average which is double
- Random rand = new Random();
- int random = rand.nextInt(15)+1; //creating new random number from 1-15
- Scanner scan = new Scanner(System.in);
- System.out.println("Welcome to Michelle's guessing game! Guess a number between 1 and 15.");
- int user = scan.nextInt(); //variable for user input
- guessing(user,random,guess,plays,totalguess);
- }
- public static void guessing(int user, int random, double guess,double plays,double totalguess) //method that determines what happens if user input equals/is less than/is greater than the actual number
- {
- if (user==random) //what happens if user input equals random number
- {
- plays=plays+1; //counting number of games played
- guess = guess+1; //counting number of guesses
- totalguess=totalguess+guess; //counting total number of guesses from all games combined
- Scanner scan4 = new Scanner(System.in);
- System.out.println("Congratulations! You guessed the number " + random + " in " + guess + " tries!");
- System.out.println("Do you want to play the game again? The computer will generate another random number from 1-15.");
- String x = scan4.next();
- if(x.equalsIgnoreCase("Y") || x.equalsIgnoreCase("YES")) //if user wants to play another game
- {
- Random rand = new Random();
- int random2 = rand.nextInt(15)+1; //generating another random number from 1-15
- Scanner scan = new Scanner(System.in);
- System.out.println("Welcome to Michelle's guessing game! Guess a number between 1 and 15.");
- guess=0; //resetting number of guesses per game
- int user2 = scan.nextInt(); //another variable for user input
- guessing(user2,random2,guess,plays,totalguess);
- }
- else //if user doesn't want to play another game, prints summary and statistics
- {
- System.out.print("You played " + plays + " games, with a grand total of " + totalguess + " guesses, and an average of " + totalguess/plays + " guesses per game.");
- }
- }
- if (user>random) //what happens if user input is greater than random number
- {
- guess = guess+1; //counting number of guesses per game
- System.out.println("Your guess was too high.");
- ask(user,random,guess,plays,totalguess);
- }
- if(user<random) //what happens if user input is less than random number
- {
- guess=guess+1; //counting number of guesses per game
- System.out.println("Your guess was too low.");
- ask(user,random,guess,plays,totalguess);
- }
- }
- public static void ask(int user, int random,double guess,double plays,double totalguess) //method that asks user if they want to guess again
- {
- Scanner scan2 = new Scanner(System.in);
- System.out.println("Do you want to guess again? Type Y or N.");
- String s = scan2.next();
- if(s.equalsIgnoreCase("Y") || s.equalsIgnoreCase("yes"))
- {
- Scanner scan3 = new Scanner(System.in);
- System.out.println("Guess another number from 1-15."); //allows user to guess again
- user = scan3.nextInt();
- guessing(user,random,guess,plays,totalguess);
- }
- else
- {
- System.out.println("You played " + plays + " games, with a grand total of " + totalguess + " guesses, and an average of " + totalguess/plays + " guesses per game."); //if the user doesn't want to guess again
- }
- }
- }
- Question 4 (computer tries to guess the number that the user has in mind) Pseudocode:
- 1. Create variables for a random number that the computer generates per game and the amount of guesses that the computer made per game. Ask the user to think of a number.
- 2. Then, the computer will ask the user if the random number generated is the number the user is thinking of.
- 3. If the random number does not equal the user's, then ask the user if it was too high or too low. If too high, then the computer generates another random number that is lower than its previous guess and repeats step 2. If too low, then the computer generates another random number that is higher than its previous guess and repeats step 2. This goes on until the random number generated equals the user's number.
- 4. If the random number equals the user's, print out a congratulations, and the total amount of times the computer had to guess before getting the user's number.
- Question 4 Code:
- //this is Michelle's code
- //11/9/16
- import java.util.*;
- public class question4
- {
- public static void main (String [] args)
- {
- Random rand = new Random();
- int comp = rand.nextInt(10)+1; //creating new random number for the computer from 1-10
- int guess = 0; //variable that counts number of guesses made by the computer
- System.out.println("Welcome to Michelle's guessing game! The computer will try to guess the user's number.");
- System.out.println("Think of a number from 1-10.");
- System.out.println("Got it? Okay, now the computer will try and guess your number.");
- guessing(guess,comp);
- }
- public static void guessing(int guess,int comp) //method making the computer guess random numbers
- {
- Scanner scan = new Scanner(System.in);
- System.out.println("Is your number " + comp + "? Type Y or N."); //asking user if the random number is the user's number
- String s = scan.next();
- ask(s,guess,comp);
- }
- public static void ask(String s, int guess,int comp) //asks user if random number was too high or low
- {
- if (s.equalsIgnoreCase("N") || s.equalsIgnoreCase("no")) //if the random number is not the user's number
- {
- guess = guess+1; //counting number of guesses made by the computer
- Scanner scan2 = new Scanner(System.in);
- System.out.println("Was my answer too high or too low?");
- String a = scan2.nextLine();
- if(a.equalsIgnoreCase("too high")) //what happens if it's too high
- {
- Random rand = new Random();
- comp = rand.nextInt(comp-1)+1; //if too high, computer then generates a number that'll be lower than its previous guess
- guessing(guess,comp);
- }
- if(a.equalsIgnoreCase("too low")) //what happens if it's too low
- {
- Random rand = new Random();
- comp = rand.nextInt(10-comp)+(comp+1); //if too low, computer then generates a number that'll be higher than its previous guess
- guessing(guess,comp);
- }
- }
- else //if random number equals user's number
- {
- guess=guess+1; //counting number of guesses made by the computer
- System.out.println("I got your number of " + comp + " correct in " + guess + " tries!");
- }
- }
- }
- Question 5 (rock, paper, scissors) Pseudocode:
- 1. Have the computer generate a random number from 0-8. If the number is between 0 and 2, inclusive, then the computer generates a string that contains "ROCK." If between 3 and 5, inclusive, then the computer generates a string that contains "PAPER." If between 6 and 8, inclusive, then the computer generates a string that contains "SCISSORS."
- 2. Ask the user to choose between rock, paper, or scissors. Compare this with the computer's generated string. Then, create if statements and instances for all 9 possibilities (user=rock, computer=rock; user=rock, computer=paper; user=rock, computer=scissors; user=paper, computer=rock; user=paper, computer=paper; user=paper, computer=scissors; user=scissors, computer=rock; user=scissors, computer=paper; user=scissors, computer=scissors). Tell the user if they won, lost, or tied.
- 3. Ask the user if they want to play again. If yes, repeat steps 1 and 2. If no, the program ends.
- Question 5 Code:
- //this is Michelle's code
- //11/9/16
- import java.util.*;
- public class question5
- {
- public static void main (String[] args)
- {
- System.out.println("Welcome to Michelle's game of Rock, Paper, and Scissors!");
- System.out.println("The computer will now generate a random choice of either rock, paper, or scissors.");
- game();
- }
- public static void game() //method that generates the computer's choice of either rock, paper, or scissors
- {
- String computer = new String(); //creating new string
- Random rand = new Random();
- int x = rand.nextInt(9); //generating random number from 0-8
- if(x>=0 && x<=2) //if number is between 0 and 2, inclusive, then the computer choose ROCK
- {
- computer = "ROCK";
- }
- if(x>=3 && x<=5) //if number is between 3 and 5, inclusive, then the computer chooses PAPER
- {
- computer = "PAPER";
- }
- if(x>=6 && x<=8) //if number is between 6 and 8, inclusive, then the computer chooses SCISSORS
- {
- computer = "SCISSORS";
- }
- Scanner scan = new Scanner(System.in);
- System.out.println("Type in your choice of rock, paper, or scissors. Remember, rock beats scissors, paper beats rock, and scissors beats paper."); //prompts user for their choice
- String answer = scan.next(); //user's answer
- result(answer,computer);
- }
- public static void result(String answer, String computer) //determines what happens for all of the possibilites
- {
- if(answer.equalsIgnoreCase("rock") && computer.equals("SCISSORS")) //if user chooses rock and computer chooses scissors
- {
- Scanner rock1 = new Scanner(System.in);
- System.out.println("Congratulations! The computer's choice was " + computer + ". You beat the computer. Want to play again? Type Y or N.");
- String a = rock1.next();
- if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
- {
- game(); //goes back and plays the game again
- }
- else //ends the program if user wants to quit
- {
- System.out.print("Okay, goodbye.");
- }
- }
- if(answer.equalsIgnoreCase("rock") && computer.equals("PAPER")) //if user chooses rock and computer chooses paper
- {
- Scanner rock2 = new Scanner(System.in);
- System.out.println("Sorry, the computer's choice was " + computer + ". You lost to the computer. Want to play again? Type Y or N.");
- String a = rock2.next();
- if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
- {
- game();
- }
- else
- {
- System.out.print("Okay, goodbye.");
- }
- }
- if(answer.equalsIgnoreCase("rock") && computer.equals("ROCK")) //if user chooses rock and computer chooses rock
- {
- Scanner rock3 = new Scanner(System.in);
- System.out.println("Both you and computer chose ROCK. It's a tie. Want to play again? Type Y or N.");
- String a = rock3.next();
- if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
- {
- game();
- }
- else
- {
- System.out.print("Okay, goodbye.");
- }
- }
- if(answer.equalsIgnoreCase("paper") && computer.equals("SCISSORS")) //if user chooses paper and computer chooses scissors
- {
- Scanner paper1 = new Scanner(System.in);
- System.out.println("Sorry, the computer's choice was " + computer + ". You lost to the computer. Want to play again? Type Y or N.");
- String a = paper1.next();
- if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
- {
- game();
- }
- else
- {
- System.out.print("Okay, goodbye.");
- }
- }
- if(answer.equalsIgnoreCase("paper") && computer.equals("PAPER")) //if user chooses paper and computer chooses paper
- {
- Scanner paper2 = new Scanner(System.in);
- System.out.println("Both you and computer chose PAPER. It's a tie. Want to play again? Type Y or N.");
- String a = paper2.next();
- if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
- {
- game();
- }
- else
- {
- System.out.print("Okay, goodbye.");
- }
- }
- if(answer.equalsIgnoreCase("paper") && computer.equals("ROCK")) //if user chooses paper and computer chooses rock
- {
- Scanner paper3 = new Scanner(System.in);
- System.out.println("Congratulations! The computer's choice was " + computer + ". You beat the computer. Want to play again? Type Y or N.");
- String a = paper3.next();
- if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
- {
- game();
- }
- else
- {
- System.out.print("Okay, goodbye.");
- }
- }
- if(answer.equalsIgnoreCase("scissors") && computer.equals("SCISSORS")) //if user chooses scissors and computer chooses scissors
- {
- Scanner scissors1 = new Scanner(System.in);
- System.out.println("Both you and computer chose SCISSORS. It's a tie. Want to play again? Type Y or N.");
- String a = scissors1.next();
- if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
- {
- game();
- }
- else
- {
- System.out.print("Okay, goodbye.");
- }
- }
- if(answer.equalsIgnoreCase("scissors") && computer.equals("PAPER")) //if user chooses scissors and computer chooses paper
- {
- Scanner scissors2 = new Scanner(System.in);
- System.out.println("Congratulations! The computer's choice was " + computer + ". You beat the computer. Want to play again? Type Y or N.");
- String a = scissors2.next();
- if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
- {
- game();
- }
- else
- {
- System.out.print("Okay, goodbye.");
- }
- }
- if(answer.equalsIgnoreCase("scissors") && computer.equals("ROCK")) //if user chooses scissors and computer chooses rock
- {
- Scanner scissors3 = new Scanner(System.in);
- System.out.println("Sorry, the computer's choice was " + computer + ". You lost to the computer. Want to play again? Type Y or N.");
- String a = scissors3.next();
- if(a.equalsIgnoreCase("Y") || a.equalsIgnoreCase("YES"))
- {
- game();
- }
- else
- {
- System.out.print("Okay, goodbye.");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment