Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class Main {
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in);
- Random random = new Random();
- int rand = 0;
- while (true){
- rand = random.nextInt(101);
- if(rand != 0) break;
- }
- int guess = 0;
- int numberOfAttempts = 0;
- int maxattempts = 7;
- System.out.println("\n I'm thinking of a number between 1-100. You have 7 guesses.");
- do{
- System.out.println("\n Guess #"+(numberOfAttempts+1)+ ":");
- guess = keyboard.nextInt();
- if(guess == rand){
- System.out.println("\n You guess correct! You are a genius");
- System.out.println("\n You guess with "+numberOfAttempts+ " attempts");
- }
- if(numberOfAttempts == maxattempts){
- System.out.println("\n Sorry, you didn't guess it in 7 tries. You lose.");
- }
- if(guess > rand){
- System.out.println("\n Sorry, that guess is too high");
- }
- else if(guess < rand){
- System.out.println("\n Sorry, you are too low");
- }
- numberOfAttempts = numberOfAttempts + 1;
- }while (guess != rand && numberOfAttempts < maxattempts);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement