Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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);
        }
    }
}