Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class NumberGuesser {
- public static int randomizer() {
- Random rand = new Random ();
- int answer = rand.nextInt(100);
- System.out.println("The computer's answer is " + answer);
- return answer;
- }
- public static String name() {
- //unnecessary; just scanner practice
- Scanner s = new Scanner(System.in);
- System.out.println("Let's play a guessing game! What's your name?");
- String userName= s.nextLine();
- System.out.println("Hi, " + userName + "! Okay, I'm thinking of a number between 1 and 100. You have ten tries to guess what it is.");
- return userName;
- }
- public static boolean guesser(int randomNumber) {
- for(int i=1;i<=10;i++) {
- Scanner s = new Scanner(System.in);
- System.out.println("Try number " + i + ". Guess a number.");
- int userGuess= s.nextInt();
- if (userGuess < randomNumber && i <= 9) {
- System.out.println("Go higher.");
- }
- else if (userGuess < randomNumber && i == 10) {
- return false;
- }
- else if (userGuess > randomNumber && i <= 9) {
- System.out.println("Go lower.");
- }
- else if (userGuess > randomNumber && i == 10) {
- return false;
- }
- else if (userGuess == randomNumber) {
- System.out.println("You got it!");
- return true;
- }
- }
- return false;
- }
- public static int scoreTracker (boolean winOrLose, String userName) {
- int pcScore;
- int playerScore;
- for (pcScore = 0, playerScore = 0; pcScore <= 100, playerScore <= 100) {
- if (winOrLose == false) {
- System.out.println("You're out of guesses. My win!");
- pcScore++;
- }
- else {
- System.out.println(userName + " wins!");
- playerScore++;
- }
- }
- return 3;
- }
- public static void main (String[] args) {
- int randomNumber = randomizer();
- String userName = name();
- boolean winOrLose = guesser(randomNumber);
- scoreTracker(winOrLose, userName);
- randomNumber = randomizer();
- }
- }
- /*Write a number guessing game. Your program should choose a random number between 0 and 99 (see below).
- * The user should then be given 10 guesses to guess the number, with your program telling them, on each guess,
- * whether they were too high, too low, or correct. To generate a random int between 0 and 99 use the following code:
- Random rand = new Random();
- int randomNum = rand.nextInt(100);
- And add the following line to the very top of your program (above the public class line):
- import java.util.Random;
- *
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement