Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- 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;
- rand = random.nextInt(100);
- System.out.println("I'm thinking of a number between 1-100. You have 7 guesses.");
- int max=8;
- int guesses=0;
- int attemps=1;
- while (guesses != rand && attemps<max) {
- System.out.println("Guess # " + attemps++ + ":");
- guesses = keyboard.nextInt();
- if (guesses == rand) {
- attemps++;
- System.out.println("You guessed correctly with" + attemps + "tries");
- } else if (guesses < rand) {
- System.out.println("Sorry, you are too low.");
- } else {
- System.out.println("Sorry, that guess is too high.");
- }
- }
- if (guesses != rand && attemps==max);
- System.out.println("Sorry, you didn't guess it in 7 tries. You lose.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement