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(101);
- if(rand !=0) break;
- }
- int entry = 0;
- int numberOfAttempts = 0;
- int maxAttempts = 7;
- while ( entry != rand && numberOfAttempts < maxAttempts) {
- System.out.println("\nYou have guessed " + numberOfAttempts + " times");
- System.out.println("I´´ thinking of a number from 1-100. Try to guess!");
- entry = keyboard.nextInt();
- if (entry < rand) {
- System.out.println("You guessed too low");
- } else if (entry > rand){
- System.out.println("You guessed too high");
- } else {
- System.out.println("You guessed correctly, with " + (numberOfAttempts + 1) + " tries.");
- }
- numberOfAttempts = numberOfAttempts + 1;
- }
- if (numberOfAttempts == maxAttempts){
- System.out.println("\nSorry you used up all of your tries, with " + numberOfAttempts + " attempts.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement