Код: Выделить всё
import java.util.Scanner;
public class numGamee {
static Scanner sc = new Scanner(System.in);
static int choose, choice;
static String[] difficulty = { "Easy", "Medium", "Hard" };
static int score = 0;
public static void gameStart() {
String[] startMenu = { "Start", "Quit" };
int index;
System.out.println(
"Welcome to WildGuess, the number guessing game! Please choose an option(Type the number to choose the option):");
for (index = 0; index < startMenu.length; index++) {
System.out.println((index + 1) + " => " + startMenu[index]);
}
choose = sc.nextInt();
}
public static void chooseDifficulty() {
int indices;
System.out.println("Please type the number for the difficulty you want to select:");
for (indices = 0; indices < difficulty.length; indices++) {
System.out.println((indices + 1) + " => " + difficulty[indices]);
}
choice = sc.nextInt();
}
public static void playGame() {
if (choice == 1) {
System.out.println(" You have chosen " + difficulty[0] + " level");
int randomNum = (int) (Math.random() * 51);
System.out.println("As you have chosen " + difficulty[0]
+ " difficulty, a random number has been chosen between 0-50. Guess the number to win the game!");
System.out.println("Please type in your guess number:");
int guessNum = sc.nextInt();
while (guessNum != randomNum) {
if (guessNum > randomNum) {
System.out.print(
"Too high! The number is lower than " + guessNum + " .Please guess the number again:");
guessNum = sc.nextInt();
} else if (guessNum < randomNum) {
System.out.print(
"Too low! The number is higher than " + guessNum + " .Please guess the number again:");
guessNum = sc.nextInt();
}
}
if (guessNum == randomNum) {
System.out.println(" Bravo! You have guessed the correct number!");
score++;
System.out.println("Your score is now " + score);
}
} else if (choice == 2) {
System.out.println(" You have chosen " + difficulty[1] + " level");
System.out.println("As you have chosen " + difficulty[1]
+ " level, a random number has been chosen between 1-100. Guess the number to win the game!");
int randomNum = (int) (Math.random() * 101);
System.out.print("Please type in your guess number:");
int guessNum = sc.nextInt();
while (guessNum != randomNum) {
if (guessNum > randomNum) {
System.out.print(
"Too high! The number is lower than " + guessNum + ".Please enter the number again:");
guessNum = sc.nextInt();
} else if (guessNum < randomNum) {
System.out.print(
"Too low! The number is higher than " + guessNum + ". Please enter the number again:");
guessNum = sc.nextInt();
}
}
if (guessNum == randomNum) {
System.out.println("Bravo!you have guessed the correct number!");
score++;
System.out.println("Your score is now " + score);
}
} else if (choice == 3) {
System.out.println(" You have chosen " + difficulty[2] + " level");
System.out.println("As you have chosen " + difficulty[2]
+ " level, a random number has been chosen between 1-500. Guess the number to win the game!");
int randomNum = (int) (Math.random() * 501);
System.out.print("Please type in your guess number:");
int guessNum = sc.nextInt();
while (guessNum != randomNum) {
if (guessNum > randomNum) {
System.out.print(
"Too high! The number is lower than " + guessNum + ".Please enter the number again:");
guessNum = sc.nextInt();
} else if (guessNum < randomNum) {
System.out.print(
"Too low! The number is higher than " + guessNum + ". Please enter the number again:");
guessNum = sc.nextInt();
}
}
if (guessNum == randomNum) {
System.out.println("Bravo!you have guessed the correct number!");
score++;
System.out.println("Your score is now " + score);
} else {
System.out.println("Incorrect input!");
}
}
}
public static void playAgain() {
System.out.println("Do you want to play the game again?(Type 1 for yes and 2 for no)");
int userPrompt = sc.nextInt();
boolean playAgain = true;
while (playAgain) {
if (userPrompt == 1) {
chooseDifficulty();
playGame();
System.out.println("Do you want to play the game again?(Type 1 for yes and 2 for no)");
userPrompt = sc.nextInt();
} else if (userPrompt == 2) {
playAgain = false;
System.out.println("Thank you for playing the game. Hope you enjoyed!");
}
}
}
public static void main(String[] args) {
try {
gameStart();
if (choose == 1) {
System.out.println("Let's begin!");
chooseDifficulty();
playGame();
playAgain();
} else if (choose == 2) {
System.out.println("Thank you for your time");
} else {
System.out.println("Error, wrong input");
}
} catch (Exception e) {
System.out.println("Please enter a numerical input!");
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... better-way