0

I'm trying to get a dice roller happening and I'm having some difficulty adding a loop somewhere so the program doesn't quit after one roll. I want to ask the user if they want to roll and it rolls by saying "y." I want to end the program by asking the user the same question but it ends with "n"

/*
    Natasha Shorrock
    Assignmnt A6
    11/07/16
     */
    package diceroller;
    import java.util.Random;
    import java.util.Scanner;

    public class DiceRoller {
        public static void main(String []args) {
            System.out.println(" Do you want to roll the dice? ");
            Random dice = new Random();
            Scanner input = new Scanner(System.in);
            int faces;
            int result;

            System.out.println("Dice Roller\n");
            System.out.println("How many faces does the dice have?");
            faces = input.nextInt();
            result = dice.nextInt(faces) + 1;
            System.out.println("\nThe dice rolled a " + result );
        }//Dice Roller
    }//class DiceRoller
Bill
  • 12,022
  • 4
  • 34
  • 54

2 Answers2

0

You have to read the input after the following expression:

System.out.println(" Do you want to roll the dice? ");

To receive the users input call: input.nextLine();. Thereafter loop while the input is "y". If the user input is not equals to "y" the while-loop gets terminated.

The while(condition) loop is excuted as long as the condition is true

The while statement continually executes a block of statements while a particular condition is true. The while and do-while Statements

For example consider this code :

public static void main(String[] args) {
    Random dice = new Random();
    Scanner input = new Scanner(System.in);
    System.out.println("Do you want to roll the dice? (y: yes / q: to quit)");
    String answer = input.nextLine(); // reading the input

    // we check if the answer is equals to "y" to execute the loop,
    // if the answer is not equals to "y" the loop is not executed
    while ("y".equals(answer)) {  
        System.out.println("Dice Roller");
        System.out.println("How many faces does the dice have?");
        int faces = input.nextInt();
        int result = dice.nextInt(faces) + 1;
        input.nextLine(); // to read the newline character (*)
        System.out.println("The dice rolled a " + result);
        System.out.println("Do you want to roll the dice? (y: yes / q: to quit)");
        answer = input.nextLine();
    }
}

To find out more about the while and do-while mechanisms, please visit this toturial

(*) To understand the use of nextLine() after a call to nextInt() please visit Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

Community
  • 1
  • 1
Younes HAJJI
  • 375
  • 3
  • 20
  • There is a lot to add to this answer. What is `while` ? Since the OP seems to be learning JAVA. Knowing that the current answer to that question is an integer, this could be a problem to use `nextLine()` – AxelH Nov 03 '16 at 14:32
  • I would like to add a part after System.out.println(" Do you want to roll the dice? "); that has a while loop that begins the program by saying "y" and ending it by saying "n" – Natasha Shorrock Nov 03 '16 at 14:34
  • please consider an explanation of the do-while loop since the OP seems to be a beginner. – Tom Wellbrock Nov 03 '16 at 14:43
  • I edited the answer to add the code, hope that the answer is clear now – Younes HAJJI Nov 03 '16 at 14:44
  • @YounesHAJJI you should edit the text, so that the user understands, that he has to enter a 'y' to restart the process. Also as I mentioned consider explaining what you are doing. Also you are writing that the programm loops while the input is not 'n' which is wrong. – Tom Wellbrock Nov 03 '16 at 14:47
  • Thank you I aprreciate it – Natasha Shorrock Nov 03 '16 at 14:48
0

A do-while is a very good option. Another way of doing it could be using a while loop with a switch or preferably an IF/ ELSE IF statement. Perhaps something like below. This is only a suggestion.

boolean checkForExit = false;
while(checkForExit != true) { //while checkForExit does not equal true (therefore false) continue..

   System.out.println("Do you want to roll the dice?");
   String answer = input.next(); //get char input from user.

   if(answer.toLowerCase().equals("y")) { //if 'y' then do this
   //ask user for number of faces on dice and roll
   } else { // otherwise end the program
      //set isTrue to true to exit while loop. ends program
      isTrue = true;
   }
}
Ryan.Brown
  • 21
  • 3
  • please consider using input.nextChar(). Otherwise use equals() to copare the terms. Also your attribute isTrue is an abonimation, please change its name. Also the programm will loop if the input is something besides 'y' or 'n' because you are jusing else if instead of the normal else – Tom Wellbrock Nov 03 '16 at 14:55
  • So where would I insert boolean and the inital while? – Natasha Shorrock Nov 03 '16 at 15:13
  • you would use this in the main method – Ryan.Brown Nov 03 '16 at 16:04