Hi all, I have created a basic guessing game as follows:
package latesttasks; // Create a basic guessing game, user has to guess 1 - 10, system prints // higher, lower, or correct. When correct answer is met, tell the user how // many attempts it took and ask if they would like to play again. import java.util.Random; import java.util.Scanner; public class Task3 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Random rand = new Random(); int number = rand.nextInt(10) + 1; int guess; System.out.println("Welcome to Dan's Guessing Game"); System.out.println("Guess the number between 1 and 10"); System.out.println(""); guess = scan.nextInt(); while (guess < number) { System.out.println("Higher!"); guess = scan.nextInt(); } while (guess > number) { System.out.println("Lower!"); guess = scan.nextInt(); } while (guess == number) { System.out.println("Correct!"); break; } } }
Now what i want to do is take this program to the next level.
Firstly I want the Program to count the user of attempts it took the user to guess correctly and print out a message like.... ''It took you 'x' attempts''
Secondly I want to add an option for the user to play again at the end, for example print: '' Would you like to play again? y / n. The user would type 'y' to play again, or 'n' to terminate the program.
Any suggestions ?
--- Update ---
Can someone please advise how to wrap my code in color coded tags rather than just plain ?
Many thanks.