Advertisement
CamiloCastilla

Untitled

Dec 9th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1. package com.company;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner keyboard = new Scanner(System.in);
  9.         Random random = new Random();
  10.         int rand = 0;
  11.  
  12.         while (true) {
  13.             rand = random.nextInt(10);
  14.             if (rand != 0) break;
  15.         }
  16.  
  17.         int numberofguesses=0;
  18.         int entry = 0;
  19.  
  20.         System.out.println("\nI have chosen a number between 1 and 10");
  21.  
  22.         do{
  23.  
  24.             System.out.print("Your guess: ");
  25.             entry = keyboard.nextInt();
  26.  
  27.             if ( entry == rand) {
  28.  
  29.             } else {
  30.                 System.out.println("That is incorrect. Guess again!");
  31.             }
  32.             numberofguesses++;
  33.         }
  34.         while (entry != rand);
  35.  
  36.         System.out.println("You are correct!");
  37.         System.out.println("It only took you " + numberofguesses + " guesses");
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement