D
D
ddssbm2021-11-03 14:20:33
Java
ddssbm, 2021-11-03 14:20:33

Console game in Dice, how to implement the ability to choose between exiting the game and continuing it?

Good day. Implemented a simple console dice game, it has a choice between three game modes - One opponent, two opponents, a game with yourself. If you lose, the game gives "Truth or Dare", but it's not that important. The game functions, happy as a cat. Unfortunately, I ran into the problem of completing the last item of the game's feature. Namely, the choice is to continue the game (thus resuming the main method of the game) or exit (not by simply pressing the red cross on the console, but by entering the conditional Yes).

package com.DiceGameTruthOrDare;

import java.util.Scanner;
import static java.lang.System.*;
import static java.lang.Math.*;

public class DiceGameTruthOrDare {

 //main method the game
    public static void main(String[] args) {
    out.println("Welcome to the game!");
    //out.println(TruthOrDare.TruthOrDare()); //array chek 
    GameMode.modeChoice();
    }
}

Here is the main class and method of the program. I tried wrapping it in a while and do while loop, nothing good came of it. Tried to implement System.exit(n) in various ways, didn't work. Tried through method recursion, etc. Nothing comes out, but I did not find a sensible answer to the account of such a branch in the console application on the Internet. Everyone writes about the same System.exit(n), but I still don’t understand how to implement it correctly. In general, I hope here they will prompt me.
PS Of course, you can just wrap the main method of the program in while and write something like "to exit the program, hold down n + n + n. But I'm interested in implementing the branch in order to understand how this can even work."

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-11-03
@ddssbm

Good afternoon.
Inside the method, a while () loop is created, inside which you listen to the input (Scanner) and if the desired command was entered, then do System.exit (n)
Well, do not forget to handle exceptions so that, for example, the application does not exit on an incorrect or non-existent command

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question