Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Guess {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- printBeginning();
- boolean playAgain = true;
- while (playAgain) {
- game();
- System.out.print("Do you want to play again? ");
- playAgain = scanner.nextLine().equals("Yes");
- }
- }
- private static void printBeginning() {
- System.out.println("This program allows you to play a guessing game");
- }
- private static void game() {
- Random rnd = new Random();
- Scanner scanner = new Scanner(System.in);
- int correctNum = rnd.nextInt(100);
- int guesses = 0;
- int guessedNum;
- System.out.println("I'm thinking of a number...");
- while (correctNum != guessedNum) {
- System.out.print("Your guess? ");
- guessedNum = scanner.nextInt();
- if (guessedNum > correctNum) {
- System.out.println("higher");
- } else if (guessedNum < correectNum) {
- System.out.println("lower");
- }
- }
- System.out.println("You got it right in " + guesses + " guesses");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement