Untitled

 avatar
unknown
plain_text
10 months ago
3.1 kB
17
Indexable
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class Main {

    static boolean isRunning = true;
    static Scanner scan = new Scanner(System.in);

    static int menuChoice;

    static Random random = new Random();

    public static void rollDice(int die_number_of_sides) {

        final int minRoll = 1;
        final int maxRoll = die_number_of_sides;

        System.out.println("Let's roll a " + die_number_of_sides + "- sided dice!");

        System.out.println("Generated numbers are within " + minRoll + " to " + maxRoll);
        int result = random.nextInt(maxRoll ) + minRoll;

        System.out.println("You rolled " + result + "!");

        if( result == maxRoll )
            System.out.println("Critical hit!!!");

        else if( result == minRoll )
            System.out.println("Critical miss!!!");
    }

    public static void main(String[] args) {

        while (isRunning) {

            System.out.println("\nWelcome to the DnD Dice Roller!");
            System.out.println("\nWhat would you like to do?");
            System.out.println("[1] Roll a D20");
            System.out.println("[2] Roll a D10");
            System.out.println("[3] Roll a D6");
            System.out.println("[4] Roll a D4");
            System.out.println("[5] Exit program");
            System.out.print("\nPlease type a number and press ENTER: ");

            try {

                Scanner input = new Scanner(System.in);
                menuChoice = input.nextInt();

                switch (menuChoice) {

                    case 1:

                        rollDice(20);

                        System.out.print("\nPress any key to continue . . . ");

                        scan.nextLine();
                        break;

                    case 2:

                        rollDice(10);

                        System.out.print("\nPress any key to continue . . . ");
                        scan.nextLine();
                        break;

                    case 3:

                        rollDice(6);

                        System.out.print("\nPress any key to continue . . . ");
                        scan.nextLine();
                        break;

                    case 4:

                        rollDice(4);

                        System.out.print("\nPress any key to continue . . . ");
                        scan.nextLine();
                        break;

                    case 5:
                        isRunning = false;
                        System.out.println("Thank you for using this program!");
                        break;


                    default:
                        System.out.println("You need to type in a number between 1 and 5!");
                        break;
                }
            }

            catch (InputMismatchException ex) {
                System.out.println("Invalid input, please use a number between 1 and 5!");
            }
        }
    }
}
Editor is loading...
Leave a Comment