Depends on whether you want some kind of user interface. You can easily create message boxes that request custom values ββand display results. An example might look like this:
boolean repeat = true;
while (repeat) {
String strTosses = JOptionPane.showInputDialog(null, "Please enter number of coin tosses", 10);
int tosses;
try {
tosses = Integer.parseInt(strTosses);
}
catch (NumberFormatException ex) {
continue;
}
for (int i = 0; i <= tosses; i++) {
int benito1 = (int)(Math.random() * 2);
int benito2 = (int)(Math.random() * 2);
System.out.println(benito1 + " " + benito2);
}
int again = JOptionPane.showConfirmDialog(null, "Do you want to try again", "Try again", JOptionPane.YES_NO_OPTION);
if (again == JOptionPane.NO_OPTION) {
repeat = false;
}
}
Integer.parseInt () may throw a NumberFormatException if the value entered is not a number. The program will catch the error and try again.
source
share