João Pedro
João Pedro

Reputation: 13

How can I apply an If ... Else in this program and how can I optimize this code, if possible?

I am new to programming in general and I was playing around a bit with Java and ended up making this code that rolls a certain number of dice that the user wants. I wanted to apply a "Play Again" system, where the user would choose whether to close the program or roll other dice. I tried using If Else but I'm having trouble and was wondering if anyone could give me some direction or solutions.


import java.util.Random;
import java.util.Scanner;

public class DiceRoll {
    public static void main(String args[])
    {
        System.out.print("Enter the number of dice: ");

        Scanner input = new Scanner(System.in);
        int numberOfDice = input.nextInt();

        // generating random numbers
        Random ranNum = new Random();

        System.out.print("You rolled: ");
        int total = 0;
        int randomNumber = 0;

        for (int i = 0; i < numberOfDice; i++) {

            // Generating the random number and storing it in the 'randomNumber' variable
            randomNumber = ranNum.nextInt(20) + 1;
            total = total + randomNumber;
            System.out.print(randomNumber);
            System.out.println(" ");
        }
        System.out.print("");
        System.out.println("Total: " + total);
        input.close();
    }
}

Upvotes: 1

Views: 56

Answers (1)

Gurkirat Singh Guliani
Gurkirat Singh Guliani

Reputation: 1015

You can just wrap your entire logic in a while loop and after the game is played, ask the user to play again or not. If he types yes, then you take him to the start else you just break out of the while loop.

    import java.util.Random;
    import java.util.Scanner;
    
    public class DiceRoll {
      public static void main(String args[]) {
        int total = 0;
        while (true) {
          System.out.print("Enter the number of dice: ");
          Scanner input = new Scanner(System.in);
          int numberOfDice = input.nextInt();
          // generating random numbers
          Random ranNum = new Random();
    
          System.out.print("You rolled: ");
    
          int randomNumber = 0;
    
          for (int i = 0; i < numberOfDice; i++) {
    
            // Generating the random number and storing it in the 'randomNumber' variable
            randomNumber = ranNum.nextInt(20) + 1;
            total = total + randomNumber;
            System.out.print(randomNumber);
            System.out.println(" ");
          }
    
          System.out.print("");
          System.out.println("Total: " + total);
          System.out.println("do you want to play again- type yes or no ");
          String s = input.next();
          if (s.equalsIgnoreCase("yes")) {
            continue;
          } else {
            input.close();
            break;
          }
        }
      }
//  public static void main(String args[]) {
//    int total = 0;
//    do {
//      System.out.print("Enter the number of dice: ");
//      Scanner input = new Scanner(System.in);
//      int numberOfDice = input.nextInt();
//      // generating random numbers
//      Random ranNum = new Random();
//
//      System.out.print("You rolled: ");
//
//      int randomNumber = 0;
//
//      for (int i = 0; i < numberOfDice; i++) {
//
//        // Generating the random number and storing it in the 'randomNumber' variable
//        randomNumber = ranNum.nextInt(20) + 1;
//        total = total + randomNumber;
//        System.out.print(randomNumber);
//        System.out.println(" ");
//      }
//
//      System.out.print("");
//      System.out.println("Total: " + total);
//      System.out.println("do you want to play again- type yes or no ");
//      String s = input.next();
//      if (s.equalsIgnoreCase("yes")) {
//        continue;
//      } else {
//        input.close();
//        break;
//      }
//    } while (true);
//  }

    }

Depending on whether you want to show the total for all the games ( then declare the total variable outside the while loop ) or you want to show the total for each game( then declare the total variable inside the for loop) The output comes in this format.

Enter the number of dice: 2
You rolled: 17 
16 
Total: 33
do you want to play again- type yes or no 
yes
Enter the number of dice: 1
You rolled: 7 
Total: 40
do you want to play again- type yes or no 
no

Upvotes: 1

Related Questions