NumberGuessingGame
package pratice; import java.util.Scanner; import java.util.Random; public class NumberGuessingGame { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Random random = new Random(); int lowerBound = 1; int upperBound = 100; int numberToGuess = random.nextInt(101); int numberOfTries = 0; int userGuess = 0; System.out.println("๐ฎ Welcome to the Number Guessing Game!"); System.out.println("I have selected a number between " + lowerBound + " and " + upperBound + "."); System.out.println("Can you guess it?"); while (userGuess != numberToGuess) { System.out.print("Enter your guess: "); userGuess = scanner.nextInt(); numberOfTries++; if (userGuess < numberToGuess) { System.out.println("Too low! Try again."); } else if (userGuess > numberToGuess) { System.out.println("Too high! Try again."); } else { System.out.println("๐ Congratulations! You guessed the number in " + numberOfTries + " attempts."); } } scanner.close(); } }
Output:
๐ฎ Welcome to the Number Guessing Game!
I have selected a number between 1 and 100.
Can you guess it?
Enter your guess: 56
Too low! Try again.
Enter your guess: 78
Too high! Try again.
Enter your guess: 65
Too low! Try again.
Enter your guess: 70
Too low! Try again.
Enter your guess: 72
Too low! Try again.
Enter your guess: 74
Too low! Try again.
Enter your guess: 76
๐ Congratulations! You guessed the number in 7 attempts.
SpringBoot NumberGuessingGame
NumberGuessingGameApplication.java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class NumberGuessingGameApplication { public static void main(String[] args) { SpringApplication.run(NumberGuessingGameApplication.class, args); } }
Controller
package com.example.demo.controller; import jakarta.servlet.http.HttpSession; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import java.util.Random; @Controller public class GameController { @GetMapping("/") public String home(HttpSession session, Model model) { if (session.getAttribute("numberToGuess") == null) { session.setAttribute("numberToGuess", new Random().nextInt(100) + 1); session.setAttribute("attempts", 0); } model.addAttribute("message", "Guess a number between 1 and 100"); return "index"; } @PostMapping("/guess") public String guess(@RequestParam int userGuess, HttpSession session, Model model) { int numberToGuess = (int) session.getAttribute("numberToGuess"); int attempts = (int) session.getAttribute("attempts"); attempts++; session.setAttribute("attempts", attempts); if (userGuess < numberToGuess) { model.addAttribute("message", "๐ฝ Too low! Try again."); } else if (userGuess > numberToGuess) { model.addAttribute("message", "๐ผ Too high! Try again."); } else { model.addAttribute("message", "๐ Correct! You guessed it in " + attempts + " attempts."); return "result"; } return "index"; } @PostMapping("/restart") public String restart(HttpSession session) { session.invalidate(); return "redirect:/"; } }
In template Create index.html,result.html
index.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Number Guessing Game</title> </head> <body> <h1>๐ฎ Number Guessing Game</h1> <p th:text="${message}"></p> <form method="post" action="/guess"> <input type="number" name="userGuess" required min="1" max="100"/> <button type="submit">Guess</button> </form> </body> </html>
result.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Result</title> </head> <body> <h2 th:text="${message}"></h2> <form method="post" action="/restart"> <button type="submit">Play Again</button> </form> </body> </html>
Result Page:
Output Page:
Top comments (0)