package Game; import java.util.Random; import java.util.Scanner; public class Game { static int countOfWonGames = 0; static int countOfGames = 0; static void printMenu() { } static int makeUserInput() { return scanner.nextInt(); } static void guessNumber() { Random random = new Random(); int randomNumber = random.nextInt(100) + 1; while (true) { int userInput = makeUserInput(); if (userInput < randomNumber) { } else if (userInput > randomNumber) { } else { break; } } countOfGames += 1; countOfWonGames += 1; } static void arithmeticBattle() { Random random = new Random(); int correctAnswers = 0; for (int i = 1; i <= 5; i++) { int a = random.nextInt(20) + 1; int b = random.nextInt(20) + 1; int operation = random.nextInt(3); int correctResult = 0; String operationSymbol = ""; if (operation == 0) { correctResult = a + b; operationSymbol = "+"; } else if (operation == 1) { if (a < b) { int temp = a; a = b; b = temp; } correctResult = a - b; operationSymbol = "-"; } else { a = random.nextInt(10) + 1; b = random.nextInt(10) + 1; correctResult = a * b; operationSymbol = "*"; } int userAnswer = makeUserInput(); if (userAnswer == correctResult) { correctAnswers++; } } countOfGames += 1; if (correctAnswers >= 3) { countOfWonGames += 1; } } static void arrayQuest() { Random random = new Random(); int arraySize = random.nextInt(10) + 5; int[] numbers = new int[arraySize]; for (int i = 0; i < arraySize; i++) { numbers[i] = random.nextInt(50) + 1; if (i < arraySize - 1) { } } int correctTasks = 0; int userMax = makeUserInput(); int realMax = numbers[0]; for (int i = 1; i < arraySize; i++) { if (numbers[i] > realMax) { realMax = numbers[i]; } } if (userMax == realMax) { correctTasks++; } else { } int userEvenCount = makeUserInput(); int realEvenCount = 0; for (int num : numbers) { if (num % 2 == 0) { realEvenCount++; } } if (userEvenCount == realEvenCount) { correctTasks++; } else { } int userSum = makeUserInput(); int realSum = 0; for (int num : numbers) { realSum += num; } if (userSum == realSum) { correctTasks++; } else { } countOfGames += 1; if (correctTasks == 3) { countOfWonGames += 1; } } static void showStatistics() { if (countOfGames > 0) { double winPercentage = (double) countOfWonGames / countOfGames * 100; } } static boolean startGame(int userInput) { if (userInput == 1) { guessNumber(); } else if (userInput == 2) { arithmeticBattle(); } else if (userInput == 3) { arrayQuest(); } else if (userInput == 4) { showStatistics(); } else if (userInput == 0) { scanner.close(); return true; } else { } return false; } static void main() { boolean needToEndGame = false; while (!needToEndGame) { printMenu(); int userInput = makeUserInput(); needToEndGame = startGame(userInput); } } }
Standard input is empty
package Game;
import java.util.Random;
import java.util.Scanner;
public class Game {
static int countOfWonGames = 0;
static int countOfGames = 0;
static Scanner scanner = new Scanner(System.in);
static void printMenu() {
System.out.println("=== Menu ===");
System.out.println("1. Угадай число");
System.out.println("2. Арифметический бой");
System.out.println("3. Массивный квест");
System.out.println("4. Статистика");
System.out.println("0. Выход");
}
static int makeUserInput() {
System.out.print(">");
return scanner.nextInt();
}
static void guessNumber() {
Random random = new Random();
int randomNumber = random.nextInt(100) + 1;
System.out.println("Загадал число от 1 до 100, попробуй угадать!");
while (true) {
int userInput = makeUserInput();
if (userInput < randomNumber) {
System.out.println("Загаданное число больше");
} else if (userInput > randomNumber) {
System.out.println("Загаданное число меньше");
} else {
System.out.println("Поздравляю, вы угадали!");
break;
}
}
countOfGames += 1;
countOfWonGames += 1;
}
static void arithmeticBattle() {
Random random = new Random();
int correctAnswers = 0;
System.out.println("Реши 5 примеров:");
for (int i = 1; i <= 5; i++) {
int a = random.nextInt(20) + 1;
int b = random.nextInt(20) + 1;
int operation = random.nextInt(3);
int correctResult = 0;
String operationSymbol = "";
if (operation == 0) {
correctResult = a + b;
operationSymbol = "+";
} else if (operation == 1) {
if (a < b) {
int temp = a;
a = b;
b = temp;
}
correctResult = a - b;
operationSymbol = "-";
} else {
a = random.nextInt(10) + 1;
b = random.nextInt(10) + 1;
correctResult = a * b;
operationSymbol = "*";
}
System.out.print(i + ". " + a + " " + operationSymbol + " " + b + " = ");
int userAnswer = makeUserInput();
if (userAnswer == correctResult) {
correctAnswers++;
}
}
System.out.println("Правильных ответов: " + correctAnswers + " из 5");
countOfGames += 1;
if (correctAnswers >= 3) {
countOfWonGames += 1;
}
}
static void arrayQuest() {
Random random = new Random();
int arraySize = random.nextInt(10) + 5;
int[] numbers = new int[arraySize];
System.out.print("Массив: [");
for (int i = 0; i < arraySize; i++) {
numbers[i] = random.nextInt(50) + 1;
System.out.print(numbers[i]);
if (i < arraySize - 1) {
System.out.print(", ");
}
}
System.out.println("]");
int correctTasks = 0;
System.out.println("Найди максимальный элемент:");
int userMax = makeUserInput();
int realMax = numbers[0];
for (int i = 1; i < arraySize; i++) {
if (numbers[i] > realMax) {
realMax = numbers[i];
}
}
if (userMax == realMax) {
System.out.println("Верно!");
correctTasks++;
} else {
System.out.println("Неверно! Максимум: " + realMax);
}
System.out.println("Сколько чётных чисел в массиве?");
int userEvenCount = makeUserInput();
int realEvenCount = 0;
for (int num : numbers) {
if (num % 2 == 0) {
realEvenCount++;
}
}
if (userEvenCount == realEvenCount) {
System.out.println("Верно!");
correctTasks++;
} else {
System.out.println("Неверно! Чётных чисел: " + realEvenCount);
}
System.out.println("Найди сумму всех элементов:");
int userSum = makeUserInput();
int realSum = 0;
for (int num : numbers) {
realSum += num;
}
if (userSum == realSum) {
System.out.println("Верно!");
correctTasks++;
} else {
System.out.println("Неверно! Сумма: " + realSum);
}
System.out.println("Правильно выполнено заданий: " + correctTasks + " из 3");
countOfGames += 1;
if (correctTasks == 3) {
countOfWonGames += 1;
}
}
static void showStatistics() {
System.out.println("=== Статистика ===");
System.out.println("Сыграно игр: " + countOfGames);
System.out.println("Выиграно игр: " + countOfWonGames);
if (countOfGames > 0) {
double winPercentage = (double) countOfWonGames / countOfGames * 100;
System.out.printf("Процент побед: %.1f%%\n", winPercentage);
}
}
static boolean startGame(int userInput) {
if (userInput == 1) {
guessNumber();
} else if (userInput == 2) {
arithmeticBattle();
} else if (userInput == 3) {
arrayQuest();
} else if (userInput == 4) {
showStatistics();
} else if (userInput == 0) {
System.out.println("Ещё увидимся!");
scanner.close();
return true;
} else {
System.out.println("Ваш ввод не был распознан, повторите ещё");
}
return false;
}
static void main() {
boolean needToEndGame = false;
while (!needToEndGame) {
printMenu();
int userInput = makeUserInput();
needToEndGame = startGame(userInput);
}
}
}