简体   繁体   中英

Java issues with for loops

I am having an issue with a dice rolling program that I'm trying to create (just uses console). Here is the dice rolling class file itself:

import java.util.Random;

public class rtd
{
    public static int[] rollthedice(int numSides, int numRolls)
    {
        int[] rollCounter = new int[numSides];
        for (int counter = 0; counter < numRolls; counter++)
        {
            Random randRoll = new Random();
            int die = randRoll.nextInt(numSides) + 1;
            if ((counter + 1) == die)
            {
                rollCounter[counter] = die;
            }
        }
        return rollCounter;
    }
}

The problem with the class is that for some reason, the for loop refuses to function when I am testing the class to see if it works with the following class in the same folder:

public class tester
{
    public static void main(String[] args)
    {
        rtd roller = new rtd();
        int[] results = new int[6];
        results = rtd.rollthedice(6, 20);
        int rollNumber = 1;
        for (int counter = 0; counter < results.length; counter++)
        {
            System.out.println(rollNumber + " " + results[counter]);
            rollNumber++;
        }
    }
}

When I run the "tester" class, the results show that only one roll was completed, meaning that the for loop did not repeat the code for the specified number of rolls. Can anyone offer me a possible solution or explanation? If you see other flaws, let me know.

I believe the issue may be in my IDE (BlueJ).

First of all, you should follow the naming conventions of the language. I know you just started. Please find time to read.

I modified your code without changing the class and method names even though I wanted to. I will leave it to you as an exercise.

Here's the modified version of rtd class. Please see the comments in source code.

public class rtd
{
    public static int[] rollthedice(int numSides, int numRolls)
    {
        // An array of total number of rolls (NOT sides) to save the result of all rolls
        int[] rollCounter = new int[numRolls];

        // Let's roll n-th times where n is numRolls
        for (int counter = 0; counter < numRolls; counter++)
        {
            // Let's get a random number between 1 to numSides (A die usually has 6 sides with 1 to 6 dots)
            int randomSide = getRand(1, numSides);

            // Let's store the current roll result in array
            rollCounter[counter] = randomSide;
        }

        return rollCounter;
    }

    /**
     * This method returns a number between a given range inclusive
     */ 
    public static int getRand(int min, int max)
    {
        return min + (int)(Math.random() * ((max - min) + 1));
    }
}

Also, you can improve your tester class like this-

public class tester
{
    public static void main(String[] args)
    {            
        int[] results = rtd.rollthedice(6, 20);

        // Since counter starts with 0, we always add 1 so we can read from 1 to 20            

        for (int counter = 0; counter < results.length; counter++)
        {
            System.out.println("Roll Number: " + (counter + 1) + " Side Picked: " + results[counter]);            
        }
    }
}

The comments in source should be pretty easy to understand. If you have questions, please ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM