0
public String roll()
    {
        Random generate = new Random();
        int diceRoll; 
        int firstNum;
        int secondNum;
        boolean winner = false;


            for (int x=1; x<=3; x++)
            {
                firstNum = generate.nextInt(6)+1;
                secondNum = generate.nextInt(6)+1;
                diceRoll = firstNum + secondNum;
                System.out.println("Roll number " + x + " was " + diceRoll);

                if(userNum == diceRoll)
                {
                    winner = true;
                    break;
                }
            }

            if(winner)
            {
                System.out.println("\nYou win!! Congratulations");
            }   
            else
            {
                System.out.println("\nYou lose");
            }


        return roll;
    }

When I run This class on my driver class at the end of the whole loop I get my numbers and all but I also get the word null at the end.
Just like this:

Roll number 1 was 6
Roll number 2 was 8
Roll number 3 was 2

You win!! Congratulations
null

3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by Taywin
0

Where is the println statement that prints out the null?
What is the value of the roll variable that is returned by the roll() method?

1

Line 34, you are returning roll variable but you didn't have anything to do with the variable inside the method. The method is supposed to build up a String and return it?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.