purpscurp
purpscurp

Reputation: 21

Diceroll Probability Method Structure

the problem I'm having is quite basic, however this is something I have not grasped very well. The program below uses recursion to calculate the probability of a given number of dice (entered by the user) totaling to a number picked by the user.

From what I understand, the method DiceRoll is a part of the class Diceroll. However when I attempt to call the method, I get an error. I believe there is something fundamentally wrong with the structure of this program. Could someone help me out?

import java.util.Scanner;

public class DiceRoll {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double dice = 0;
        int r = 0;
        System.out.println("Please input the number of dice you wish to roll between 0 and 25: ");
        if (in.nextInt() < 0 || in.nextInt() > 25){
            System.out.println("invalid number of dice");
        } else {
            dice = in.nextInt();            
        }
        System.out.println("What number between 0 and 125 do you wish to roll?");
        if (in.nextInt() < 0 || in.nextInt() > 125) {
            System.out.println("invalid number, please choose between 0 and 125");
        } else {
            r = in.nextInt();
        }
    }

    double DiceRoll(double dice,int r) {
        if (dice==1 && (r<1 || r>6)){ 
            return 0;
        }
        if (dice==1 && (r>=1 && r<=6)){
            return (1.0/6);
        } else {
            return ((1.0/6)*DiceRoll(dice-1,r-1));
        }
    }
}

DiceRoll(dice, r)

Upvotes: 0

Views: 123

Answers (3)

Rob Watts
Rob Watts

Reputation: 7146

You need to remove the line

DiceRoll(dice, r)

In addition, rather than using

double DiceRoll(double dice,int r)

which would require you to instantiate an object, use

static double RollDice(double dice, int r)

As a static method, you don't have to instantiate an object of that type in order for it to work. I've also renamed it so that the compiler won't complain about it looking like an invalid constructor method. With a static method, you would simply call

RollDice(dice, r)

at the end of your main method.

There's also another problem with your code - the input isn't going to behave like you are wanting. I assume you want it to check if the input is valid, and then ask again if it is not. Currently, it will say it is not valid then do nothing, but when it is valid it will immediately expect another number. Change your statements to look like this:

System.out.println("Please input the number of dice you wish to roll between 0 and 25: ");
dice = in.nextInt();
while (dice < 0 || dice > 25){
    System.out.println("invalid number of dice");
    dice = in.nextInt();            
}

This will only get another number if they entered an invalid number, and will loop until they give a valid input.

Upvotes: 0

Neil Locketz
Neil Locketz

Reputation: 4318

remove Diceroll method from inside the main method ^_^
EDIT: realized the DiceRoll method is not declared inside the main method but the call to it is placed outside of the main method, move it inside and it should work

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61512

All code in Java needs to be contained within a method or class. You can't just have a call to DiceRoll floating in the middle of your class.

What you really want to do is get your input from the user like you are currently, then inside the main method call DiceRoll.

Upvotes: 2

Related Questions