File tree Expand file tree Collapse file tree 1 file changed +9
-3
lines changed
src/main/java/com/thealgorithms/puzzlesandgames Expand file tree Collapse file tree 1 file changed +9
-3
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Simple Number Guessing Game.
3+ * The player guesses a random number between 1 and 100.
4+ * Reference: https://en.wikipedia.org/wiki/Guess_the_number
5+ */
6+
17package com .thealgorithms .puzzlesandgames ;
28import java .util .Scanner ;
39
410public class NumberGuess {
511 public static void playGame () {
612 Scanner sc = new Scanner (System .in );
7- int number = (int )(Math .random () * 100 ) + 1 ;
13+ int number = (int ) (Math .random () * 100 ) + 1 ;
814 int guess = 0 , tries = 0 ;
915
1016 System .out .println ("🎯 Welcome to the Number Guessing Game!" );
1117 System .out .println ("I've picked a number between 1 and 100. Try to guess it!\n " );
1218
1319 while (true ) {
1420 System .out .print ("Enter your guess: " );
15-
21+
1622 if (!sc .hasNextInt ()) {
1723 System .out .println ("Please enter a valid number." );
1824 sc .next ();
@@ -45,4 +51,4 @@ public static void main(String[] args) {
4551 playGame ();
4652 System .out .println ("\n Thanks for playing! 👋" );
4753 }
48- }
54+ }
You can’t perform that action at this time.
0 commit comments