Skip to content

Instantly share code, notes, and snippets.

@MadMaxMcKinney
Created July 10, 2013 14:17
Show Gist options
  • Save MadMaxMcKinney/5966675 to your computer and use it in GitHub Desktop.
Save MadMaxMcKinney/5966675 to your computer and use it in GitHub Desktop.
###Daily Programmer - July 10th 2013 User input in the form of 'ndm' where n is the number of dice, and m is the number of faces.
import java.util.Scanner;
public class DiceRoll {
public static void main(String[] args) {
// Get the java console scanner. Allows the user to type into the console.
Scanner s = new Scanner(System.in);
// Pull the contents from what the user said. Split the array at the "d".
// The input will look like ex: 4d10
// Returns 4, 10 to the String array 'in'.
String[] in = s.next().split("d");
// We are parsing the ints so each iteration through the for loop won't
// reparse them.
int[] val = { Integer.parseInt(in[0]), Integer.parseInt(in[1]) };
// Loop through the number of dice "val[0]" and then print what the dice roll
// was based on how many faces the dice has "val[1]".
for(int i=0; i < val[0]; i++) {
System.out.print((int)(Math.random() * val[1] + 1) + "\t");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment