Welcome to Dream.In.Code
Become a Java Expert!

Join 149,596 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,878 people online right now. Registration is fast and FREE... Join Now!




Need Help With Dice Program

 
Reply to this topicStart new topic

Need Help With Dice Program

JackSkell26
17 Oct, 2007 - 02:40 PM
Post #1

New D.I.C Head
*

Joined: 24 Sep, 2007
Posts: 19


My Contributions
Hello Everybody. This code is suppose to add ask the user how many dice they would like to roll, and how many times to roll them. Eventually I will need to use an array to store the roll numbers, so I can print out how many times each number turned up. But right now, I'm just trying to get the rolls to work. However, the total of the rolls is turning up as one number instead of the amount the user enters. Any help would be appreciated.

CODE


import java.util.*;

public class Dice2 {

    public static Scanner in = new Scanner (System.in);

    public static void main (String[] args) {

        int dice = 0;
        int roll = 0;

        while (true) {

               System.out.print ("How Many Dice Do You Want To Roll?  ");
                   dice = in.nextInt();
                       if (dice > 0) break;
               System.out.println ("Must Be Positive!");

          }

        while (true) {
               System.out.print ("How Many Times Do You Want To Roll?  ");
                   roll = in.nextInt();
                       if (roll > 0) break;
               System.out.println ("Must Be Positive!");

          }

        int dicetotal = Dicecount (dice, roll);
        System.out.println (dicetotal);

    }

    public static int Dicecount (int dice, int roll) {

        int dicetotal = 0;

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

          for (int x = 0; x < dice; x++) {

            int rollcount =(int)(1+6*(Math.random()));
            dicetotal+=rollcount; }

            return dicetotal;

    }

}


User is offlineProfile CardPM
+Quote Post

barker1
RE: Need Help With Dice Program
17 Oct, 2007 - 02:57 PM
Post #2

New D.I.C Head
*

Joined: 25 Jul, 2007
Posts: 9


My Contributions
QUOTE(JackSkell26 @ 17 Oct, 2007 - 03:40 PM) *

Hello Everybody. This code is suppose to add ask the user how many dice they would like to roll, and how many times to roll them. Eventually I will need to use an array to store the roll numbers, so I can print out how many times each number turned up. But right now, I'm just trying to get the rolls to work. However, the total of the rolls is turning up as one number instead of the amount the user enters. Any help would be appreciated.

CODE


import java.util.*;

public class Dice2 {

    public static Scanner in = new Scanner (System.in);

    public static void main (String[] args) {

        int dice = 0;
        int roll = 0;

        while (true) {

               System.out.print ("How Many Dice Do You Want To Roll?  ");
                   dice = in.nextInt();
                       if (dice > 0) break;
               System.out.println ("Must Be Positive!");

          }

        while (true) {
               System.out.print ("How Many Times Do You Want To Roll?  ");
                   roll = in.nextInt();
                       if (roll > 0) break;
               System.out.println ("Must Be Positive!");

          }

        int dicetotal = Dicecount (dice, roll);
        System.out.println (dicetotal);

    }

    public static int Dicecount (int dice, int roll) {

        int dicetotal = 0;

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

          for (int x = 0; x < dice; x++) {

            int rollcount =(int)(1+6*(Math.random()));
            dicetotal+=rollcount; }

            return dicetotal;

    }

}





try returning after you complete both loops in the Dicecount routine, you are only going thru the loops 1 times

This post has been edited by barker1: 17 Oct, 2007 - 03:00 PM
User is offlineProfile CardPM
+Quote Post

JackSkell26
RE: Need Help With Dice Program
17 Oct, 2007 - 03:28 PM
Post #3

New D.I.C Head
*

Joined: 24 Sep, 2007
Posts: 19


My Contributions
QUOTE(barker1 @ 17 Oct, 2007 - 03:57 PM) *

try returning after you complete both loops in the Dicecount routine, you are only going thru the loops 1 times


Not sure if I understand how to do this without it still adding all the values up sad.gif .

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:27PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month