Do not judge strictly, I teach Java for the third day, I decided to create a program for the fan, after the game is over, I want the question to appear: do you want to continue or not? If the answer is yes, then the game would start anew, if the answer is no, then all the best.

import java.util.Scanner; import java.util.Random; public class Bender { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int summa; String stavka; System.out.println("Сделайте вашу ставку, вы можете поставить на красное или черное, четное или не не четное, или на определленое число."); stavka = scanner.next(); System.out.println("Какую сумму ставите?"); summa = scanner.nextInt(); for (int i = 0; i < 1; i++) { int b; String[] colour = {"Черное.", "Красное."}; Random random = new Random(); Random random1 = new Random(); int select = random.nextInt(colour.length); b = (random1.nextInt(36)); if (b == 0) { System.out.print(""); } else if ((b & 1) == 0) { System.out.print(" Выпало четное" + " "); } else { System.out.print("Выпало нечетное" + " "); } System.out.println(b + " " + colour[select]); } String otvet; int znach = 1; if (stavka.equals("Красное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else if (stavka.equals("Черное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else if (stavka.equals("Не четное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else if (stavka.equals("Четное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else System.out.println("Вы проиграли,делаем ставку еще раз?"); otvet = scanner.next(); if (otvet.equals("Да")) { System.out.println("Продолжаем!"); } else if (otvet.equals("Нет")) ; System.out.println("Жаль(До Встречи!"); } } 

Closed due to the fact that off-topic participants vp_arth , αλεχολυτ , Mikhail Vaysman , ߊߚߤߘ , Denis Bubnov 24 Apr '17 at 14:26 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - vp_arth, αλεχολυτ, Mikhail Vaysman, ߊߚߤߘ, Denis Bubnov
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    And the question is what? - Regent
  • @Roman - you need to write not Java , but Java - Barmaley

2 answers 2

Alternatively, you can put a round into a separate function, and call it until the global variable true (did not check for ide):

 import java.util.Scanner; import java.util.Random; public class Bender { static boolean playAgain = true; public static void main(String[] args) { while (playAgain) { Game(); /* вызов игры */ } } public static void Game() { /* весь остальной код */ playAgain = false; //тут храним ответ игрока - да или нет System.out.println("Вы проиграли,делаем ставку еще раз?"); otvet = scanner.next(); if (otvet.equals("Да")) { System.out.println("Продолжаем!"); playAgain = true; } else if (otvet.equals("Нет")) ; System.out.println("Жаль(До Встречи!"); } } } 

The method is not quite beautiful, but with minimal code correction.

  • Does not work, play again is highlighted in red when I move the cursor, it says: Non-static field 'playAgain' cannot be referenced from a static context - Roman
  • @Roman add the word static before boolean , see the answer. - Denis

You can use the following approach to implement this logic.

  1. Wrap your game in a class, for example Game . In the loop, create an object of this class.
  2. To start the game, call the appropriate method, for example start
  3. After the game is completed, exit the start method. Then ask the player if he wants to continue.
    If the question is in the affirmative, we create the object again and proceed to step 2 , otherwise we end the program by exiting the loop .

On pseudocode, it will look like this:

 while(true){ Game game = new Game(); game.start(); System.out.println("хотите сыграть еще?") if (ответ отрицательный) break; }