Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:54:38+00:00 2026-06-12T17:54:38+00:00

I am trying to write a method rollDice(int number, int nSides) which returns the

  • 0

I am trying to write a method rollDice(int number, int nSides) which returns the total result of rolling the number dice with nSides sides.

So for example rollDice(3, 6) should return the result of rolling 3 six-sided dice (adding to a number between 3 and 18 inclusive).

Below method returns negative numbers when I type 1 for int number what do I need to do to fix this?

public static  int rollDice(int number, int nSides) {
    int num = 0;
      if(nSides >=3)
    {
      for(int i = 0; i < number; i++){
       Random  r = new Random(); 
       int roll = r.nextInt();
       num = num + (roll % nSides)+1;

      }
    }
      else{
          System.out.println("Error num needs to be from 3");

    }
    return num; 
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:54:39+00:00Added an answer on June 12, 2026 at 5:54 pm

    You only need to initialize Random r and int roll once each so I have removed them from the loop. The nextInt(int) method picks an integer from and including 0 to but not including the int. This is known as 0 (inclusive) to int (exclusive), so you have to add 1 to adjust the range to the die. You seem to have known that though I don’t know why you used %. Using * to multiply would give you the same number for all the dice which I don’t believe you mean to do. Here is one possible implementation of your class:

    import java.util.Random;
    
    public class Dice {
    
        public static  int rollDice(int number, int nSides)
        { 
            int num = 0;
            int roll = 0;
            Random  r = new Random(); 
            if(nSides >=3) 
            { 
                for(int i = 0; i < number; i++)
                { 
                    roll = r.nextInt(nSides)+1;
                    System.out.println("Roll is:  "+roll);
                    num = num + roll; 
                } 
            } 
            else
            { 
                System.out.println("Error num needs to be from 3"); 
            } 
            return num;  
        } 
    
        public static void main(String[] args)
        {
            System.out.println("Total is: "+rollDice(3, 6));
        }
    }
    /*
    Roll is:  4
    Roll is:  1
    Roll is:  2
    Total is: 7
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a method which takes a Map[K, Collection[V]] and converts it
I am trying to write a method that returns a methodinfo class from a
I am trying to write a method which tries to retrieve an object based
I'm trying to write a method named isLowerThanFreezing that returns true if the Temperature
I am trying to write this method, which keeps reading from the user, until
I am trying to write a method which receives a serializable object (implements Serializable)
I am trying to write a method for a Compact Framework 2.0 dll which
I'm trying to write a method which accepts any type of collection CC[_] and
I'm trying to write a method that will determine which computers in my domain
I am trying to write a method which takes a file path and a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.