You need a code for a mini-Game?

Here is a script in Java for a mini-game called “Guess the Number”:

javaCopy codeimport java.util.Scanner;
import java.util.Random;

public class GuessTheNumber {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Random rand = new Random();
    int number = rand.nextInt(100) + 1; 
    int guess;
    int tries = 0;

    System.out.println("I've chosen a number between 1 and 100. Can you guess what it is?");

    do {
      System.out.print("Your guess: ");
      guess = sc.nextInt();
      tries++;

      if (guess > number) {
        System.out.println("Too high. Try again.");
      } else if (guess < number) {
        System.out.println("Too low. Try again.");
      }
    } while (guess != number);

    System.out.println("You got it in " + tries + " tries!");
  }
}

In this game, a random number between 1 and 100 is generated, and the player is asked to guess what it is. The game continues until the player correctly guesses the number, and the number of tries it took is displayed at the end.


}

Leave a Comment