0
public static void main(String[] args) { int i = 0; i = rollDice(11); System.out.print(" \nThe number of rolls it took: " + i); i = rollDice(5); System.out.print(" \nThe number of rolls it took: " + i); } public static int rollDice(int desiredNum) { int dice1 = 0; int dice2 = 0; Random rand = new Random(); int sum = 0; int count = 0; do { dice1 = rand.nextInt(6) +1; dice2 = rand.nextInt(6) +1; sum = dice1 + dice2; count++; } while(sum != desiredNum); return count; } 

}

Im wanting to make it where the user can enter their own desired sum of the numbers to be rolled .Also I'm wanting it to display the value of each rolled die as its rolled. It needs to allow the user to call the rollDice method as many times as they want to.

Heres my exmaple output

EX- Please enter the Desired number: 8 Roll 1: 4 6 Sum: 10 Roll 2: 3 5 Sum: 8 It took 2 rolls to get the Desired number.

The original code above WAS a lab i had to do a few weeks ago. But we have just started this. And im trying to get ahead of the class. And this community helps alot. Thanks in advance.

3 Answers 3

2

The simplest solution here is to read user input using a Scanner, until the user enters a nominated character which ends the program.

e.g.

public static void Main(String[] args) { Scanner scan = new Scanner(System.in); do { System.out.println("Enter desired number:"); String in = scan.nextLine(); rollDice(Integer.parseInt(in)); // Implement console output formatting here } while(!in.equalsIgnoreCase("q")) } 

Here, the user can roll the dice for their desired number as many times as they want. When they are finished, entering "q" or "Q" in the console will end the program.

Also see the Javadoc for Scanner.

Sign up to request clarification or add additional context in comments.

4 Comments

+1 for do..while post-condition looping directly on the value of the input (ignoring case) instead of storing a new variable or using a pre-condition while loop. Efficiency
Where I am lost is where would I add this in the code above? Would I add it in Main or rollDice?
The do-while loop is your main function. This is the part of the program you want your user interacting with. See my edit.
I get an error ";" expected }while(while(!in.equalsIgnoreCase("q"))
0

Try separating it into a few different methods like this. It'll help you think about the problem in smaller parts.

public static void main(String[] args) { String input = ""; while(true) { //Request input System.out.println("Please enter the Desired number:"); input = getInput(); //Try to turn the string into an integer try { int parsed = Integer.parseInt(input); rollDice(parsed); } catch (Exception e) { break; //Stop asking when they enter something other than a number } } } private static String getInput() { //Write the method for getting user input } private static void rollDice(int desiredNum) { //Roll the dice and print the output until you get desiredNum } 

2 Comments

Jonn is this all going under Main?
So the "private static String getInput()" is its own Method i think i said that right?
0

To repeat, add a statement where the user enters a character that determines whether or not the program repeats itself. For example:

char repeat = 'Y'; while (repeat == 'Y' || repeat == 'y') { // Previous code goes here System.out.println("Try again? {Y/N} --> "); String temp = input.nextLine(); repeat = temp.charAt(0); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.