Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class MyClass {
- public static void main(String args[]) {
- Scanner in = new Scanner(System.in);
- Random r = new Random();
- int userInput = 0;
- int numTries = 0;
- int randomNum = r.nextInt(10) + 1;
- boolean win = false;
- while (!win) {
- userInput = in.nextInt();
- numTries++;
- if (randomNum == userInput) {
- win = true;
- }else {
- System.out.println("Sorry, please enter another number:");
- }
- }
- System.out.println("Congratulations! you have guessed the number correctly!");
- System.out.println("You took " + numTries + " number of tries");
- System.out.println(randomNum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement