Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Fabian Rodriguez
PLUS
Fabian Rodriguez
Courses Plus Student 859 Points

How should I start a Random schedule generator with specifications?

I would like to create a java program that can assign random hours to employees for example.

employeeA Monday at 12pm Tuesday 1pm etc....

1 Answer

Ronald Williams
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ronald Williams
Java Web Development Techdegree Graduate 25,021 Points

Here is one way you could do that. I hope this helps get you started with that.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        //Make an ArrayList to hold the random hours
        List<Double> randomHours = new ArrayList<>();
        //Number of employers to determine how many random hours
        int numberOfEmployes = 25;
        for (int i = 0; i < numberOfEmployes;  i++) {
            //Make a random number 1-8
            double random = Math.random() * 8 + 1;
            //check to see if > 8
            if (random > 8) random = 8;
            //add the random number to ArrayList
            randomHours.add(random);
            //Print random number
            System.out.printf("%.2f%n", random);
        }
    }
}