fork download
  1. package Game;
  2.  
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class Game {
  7.  
  8. static int countOfWonGames = 0;
  9. static int countOfGames = 0;
  10. static Scanner scanner = new Scanner(System.in);
  11.  
  12. static void printMenu() {
  13. System.out.println("=== Menu ===");
  14. System.out.println("1. Угадай число");
  15. System.out.println("2. Арифметический бой");
  16. System.out.println("3. Массивный квест");
  17. System.out.println("4. Статистика");
  18. System.out.println("0. Выход");
  19. }
  20.  
  21. static int makeUserInput() {
  22. System.out.print(">");
  23. return scanner.nextInt();
  24. }
  25.  
  26. static void guessNumber() {
  27.  
  28. Random random = new Random();
  29. int randomNumber = random.nextInt(100) + 1;
  30.  
  31. System.out.println("Загадал число от 1 до 100, попробуй угадать!");
  32.  
  33. while (true) {
  34.  
  35. int userInput = makeUserInput();
  36.  
  37. if (userInput < randomNumber) {
  38. System.out.println("Загаданное число больше");
  39. } else if (userInput > randomNumber) {
  40. System.out.println("Загаданное число меньше");
  41. } else {
  42. System.out.println("Поздравляю, вы угадали!");
  43. break;
  44. }
  45.  
  46. }
  47.  
  48. countOfGames += 1;
  49. countOfWonGames += 1;
  50.  
  51. }
  52.  
  53. static void arithmeticBattle() {
  54.  
  55. Random random = new Random();
  56. int correctAnswers = 0;
  57.  
  58. System.out.println("Реши 5 примеров:");
  59.  
  60. for (int i = 1; i <= 5; i++) {
  61.  
  62. int a = random.nextInt(20) + 1;
  63. int b = random.nextInt(20) + 1;
  64. int operation = random.nextInt(3);
  65.  
  66. int correctResult = 0;
  67. String operationSymbol = "";
  68.  
  69. if (operation == 0) {
  70. correctResult = a + b;
  71. operationSymbol = "+";
  72. } else if (operation == 1) {
  73. if (a < b) {
  74. int temp = a;
  75. a = b;
  76. b = temp;
  77. }
  78. correctResult = a - b;
  79. operationSymbol = "-";
  80. } else {
  81. a = random.nextInt(10) + 1;
  82. b = random.nextInt(10) + 1;
  83. correctResult = a * b;
  84. operationSymbol = "*";
  85. }
  86.  
  87. System.out.print(i + ". " + a + " " + operationSymbol + " " + b + " = ");
  88. int userAnswer = makeUserInput();
  89.  
  90. if (userAnswer == correctResult) {
  91. correctAnswers++;
  92. }
  93. }
  94.  
  95. System.out.println("Правильных ответов: " + correctAnswers + " из 5");
  96.  
  97. countOfGames += 1;
  98. if (correctAnswers >= 3) {
  99. countOfWonGames += 1;
  100. }
  101.  
  102. }
  103.  
  104. static void arrayQuest() {
  105.  
  106. Random random = new Random();
  107. int arraySize = random.nextInt(10) + 5;
  108. int[] numbers = new int[arraySize];
  109.  
  110. System.out.print("Массив: [");
  111. for (int i = 0; i < arraySize; i++) {
  112. numbers[i] = random.nextInt(50) + 1;
  113. System.out.print(numbers[i]);
  114. if (i < arraySize - 1) {
  115. System.out.print(", ");
  116. }
  117. }
  118. System.out.println("]");
  119.  
  120. int correctTasks = 0;
  121.  
  122. System.out.println("Найди максимальный элемент:");
  123. int userMax = makeUserInput();
  124. int realMax = numbers[0];
  125. for (int i = 1; i < arraySize; i++) {
  126. if (numbers[i] > realMax) {
  127. realMax = numbers[i];
  128. }
  129. }
  130. if (userMax == realMax) {
  131. System.out.println("Верно!");
  132. correctTasks++;
  133. } else {
  134. System.out.println("Неверно! Максимум: " + realMax);
  135. }
  136.  
  137. System.out.println("Сколько чётных чисел в массиве?");
  138. int userEvenCount = makeUserInput();
  139. int realEvenCount = 0;
  140. for (int num : numbers) {
  141. if (num % 2 == 0) {
  142. realEvenCount++;
  143. }
  144. }
  145. if (userEvenCount == realEvenCount) {
  146. System.out.println("Верно!");
  147. correctTasks++;
  148. } else {
  149. System.out.println("Неверно! Чётных чисел: " + realEvenCount);
  150. }
  151.  
  152. System.out.println("Найди сумму всех элементов:");
  153. int userSum = makeUserInput();
  154. int realSum = 0;
  155. for (int num : numbers) {
  156. realSum += num;
  157. }
  158. if (userSum == realSum) {
  159. System.out.println("Верно!");
  160. correctTasks++;
  161. } else {
  162. System.out.println("Неверно! Сумма: " + realSum);
  163. }
  164.  
  165. System.out.println("Правильно выполнено заданий: " + correctTasks + " из 3");
  166.  
  167. countOfGames += 1;
  168. if (correctTasks == 3) {
  169. countOfWonGames += 1;
  170. }
  171.  
  172. }
  173.  
  174. static void showStatistics() {
  175. System.out.println("=== Статистика ===");
  176. System.out.println("Сыграно игр: " + countOfGames);
  177. System.out.println("Выиграно игр: " + countOfWonGames);
  178.  
  179. if (countOfGames > 0) {
  180. double winPercentage = (double) countOfWonGames / countOfGames * 100;
  181. System.out.printf("Процент побед: %.1f%%\n", winPercentage);
  182. }
  183. }
  184.  
  185. static boolean startGame(int userInput) {
  186.  
  187. if (userInput == 1) {
  188. guessNumber();
  189. } else if (userInput == 2) {
  190. arithmeticBattle();
  191. } else if (userInput == 3) {
  192. arrayQuest();
  193. } else if (userInput == 4) {
  194. showStatistics();
  195. } else if (userInput == 0) {
  196. System.out.println("Ещё увидимся!");
  197. scanner.close();
  198. return true;
  199. } else {
  200. System.out.println("Ваш ввод не был распознан, повторите ещё");
  201. }
  202.  
  203. return false;
  204.  
  205. }
  206.  
  207. static void main() {
  208.  
  209. boolean needToEndGame = false;
  210.  
  211. while (!needToEndGame) {
  212. printMenu();
  213. int userInput = makeUserInput();
  214. needToEndGame = startGame(userInput);
  215. }
  216. }
  217.  
  218. }
Success #stdin #stdout 0.02s 25144KB
stdin
Standard input is empty
stdout
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);
        }
    }

}