Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

Explain the concept of java dice roll?

Asked by Kato Saito Oct 10, 2022 1.5K views 1 answer
Share

About this question

 I've written a basic dice program in Java which takes a number of faces then randomly selects a number within that boundary. Here is the code:

package com.georgegibson.diceroller;

import java.util.Random;
import java.util.Scanner;
public class DiceRoller
{
    public static void main(String []args)
    {
        Random dice = new Random();
        Scanner input = new Scanner(System.in);
        int faces;
        int result;
       System.out.println("Dice Rollern");
        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 + ".");
    }
}

Seeing as I am pretty new to Java (I come from C/C++ land), I should think this can be improved, but how?


Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.