Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- System.out.println("The best number guessing game ever!!");
- Scanner keyboard = new Scanner(System.in);
- Random Random = new Random();
- int rand = 0;
- while (true) {
- rand = Random.nextInt(100);
- if (rand != 0) break;
- }
- int guess = 0;
- int Attempts = 0;
- int Maximum = 7;
- System.out.println("I am thinking of a number between 1 to 100. Try to guess it!");
- while (guess != rand && Attempts < Maximum) {
- System.out.println("You have guessed " + (Attempts) + " times.");
- guess = keyboard.nextInt();
- if (guess < rand) {
- System.out.println("You guessed too low.");
- } else if (guess > rand) {
- System.out.println("You guessed too high.");
- } else {
- int sum = Attempts + 1;
- System.out.println("You guessed correctly, with " + (sum) + " tries.");
- }
- Attempts = Attempts + 1;
- if (Attempts != (Maximum)) {
- System.out.println("Try again.");
- }
- }
- if (Attempts == Maximum) ;
- System.out.println("Sorry, you used up all your tries, with " + Attempts + " tries");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement