Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class GuessMyNumber{
- public static void main(String[] args) {
- int guess;
- Random rand = new Random();
- Scanner scanner = new Scanner(System.in);
- boolean win = false;
- Scanner ans = new Scanner(System.in);
- boolean play = true;
- while (play == true){
- int numberOfTries = 0;
- int numberToGuess = (rand.nextInt(100)+1);
- win = false;
- while (win == false){
- System.out.println("What number am I thinking off? \n Tip: it's between 1 and 100");
- guess = scanner.nextInt();
- numberOfTries++;
- play = false;
- if (guess < numberToGuess){
- System.out.println("Your guess is to low, try again");
- } else if (guess > numberToGuess){
- System.out.println("Your guess is to high, try again");
- } else if (guess == numberToGuess){
- win = true;
- System.out.println("Congratulations, you where correct and it only took you " + numberOfTries + " tries");
- System.out.println("Would you like to play again? (y/n)");
- String answer = ans.nextLine();
- if (answer.equalsIgnoreCase("y")) {
- play = true;
- } else if (answer.equalsIgnoreCase("n")){
- System.out.println("Thank you for playing!");
- scanner.close();
- ans.close();
- break;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement