I have the following code which I wrote as a big main function: package dicegame
ID: 3911907 • Letter: I
Question
I have the following code which I wrote as a big main function:
package dicegame;
import java.util.Random;
public class RollingDice
{
public static void main(String [] args) {
//Creating the list of 4 options available
System.out.println("1. Roll a die");
System.out.println("2. Roll n dice");
System.out.println("3. Play a dice game!");
System.out.println("4. Exit");
//getting input from the player to make menu choice
int choice = Console.getInt("Make a choice");
Random choice1 = new Random();
//if player selects option 1
if (choice == 1) {
int numberOfSides = Console.getInt("Enter the number of sides");
int randomNumber = choice1.nextInt(numberOfSides)+1;//adding one to the remove the possibility of rolling a zero
System.out.println("Rolling...");
System.out.println("A " + randomNumber + " comes up");
}
//if player selects option 2
else if (choice == 2) {
int numberOfSides = Console.getInt("Enter the number of sides");
int rollingTheDice = Console.getInt("Enter a number of dice");
for (int i = 1; i <= rollingTheDice;i++) {
System.out.println("Rolling....");
int randomNumber = choice1.nextInt(numberOfSides)+1;
System.out.println("A " + randomNumber + " comes up");
}
}
//if player picks option 3
else if (choice == 3) {
int randomNumber = choice1.nextInt(10)+1;
int randomNumber2 = choice1.nextInt(10)+1;
System.out.println("You rolled " + randomNumber);
System.out.println("You rolled " + randomNumber2);
if (randomNumber != randomNumber2) {
System.out.println("You win!");
}
if (randomNumber == 1 && randomNumber2 == 1) {
System.out.println("Snake eyes, you lose!");
}
if (randomNumber == randomNumber2) {
System.out.println("Same, you lose!");
}
}
//player picks option 4
else if (choice == 4) {
System.exit(0);
}
//player picks option 5
else if (choice == 5) {
System.out.println("Bad menu choice!");
System.exit(0);
}
}
How can I break up each option into individua functions that will work in the same way?
Explanation / Answer
// I've moved the code to 5 different methods for each action and highlighted wherever required.
package dicegame;
import java.util.Random;
public class RollingDice
{
public static void one() {
int numberOfSides = Console.getInt("Enter the number of sides");
int randomNumber = choice1.nextInt(numberOfSides) + 1; //adding one to the remove the possibility of rolling a zero
System.out.println("Rolling...");
System.out.println("A " + randomNumber + " comes up");
}
public static void two() {
int numberOfSides = Console.getInt("Enter the number of sides");
int rollingTheDice = Console.getInt("Enter a number of dice");
for (int i = 1; i <= rollingTheDice; i++) {
System.out.println("Rolling....");
int randomNumber = choice1.nextInt(numberOfSides) + 1;
System.out.println("A " + randomNumber + " comes up");
}
}
public static void three() {
int randomNumber = choice1.nextInt(10) + 1;
int randomNumber2 = choice1.nextInt(10) + 1;
System.out.println("You rolled " + randomNumber);
System.out.println("You rolled " + randomNumber2);
if (randomNumber != randomNumber2) {
System.out.println("You win!");
}
if (randomNumber == 1 && randomNumber2 == 1) {
System.out.println("Snake eyes, you lose!");
}
if (randomNumber == randomNumber2) {
System.out.println("Same, you lose!");
}
}
public static void four() {
System.exit(0);
}
public static void five() {
System.out.println("Bad menu choice!");
System.exit(0);
}
public static void main(String[] args) {
//Creating the list of 4 options available
System.out.println("1. Roll a die");
System.out.println("2. Roll n dice");
System.out.println("3. Play a dice game!");
System.out.println("4. Exit");
//getting input from the player to make menu choice
int choice = Console.getInt("Make a choice");
Random choice1 = new Random();
//if player selects option 1
if (choice == 1) {
one();
}
//if player selects option 2
else if (choice == 2) {
two();
}
//if player picks option 3
else if (choice == 3) {
three();
}
//player picks option 4
else if (choice == 4) {
four();
}
//player picks option 5
else if (choice == 5) {
five();
}
}
}