Question: Can someone break down this code for me? Aren't boolean values initialized as false by default? Why is the while loop set to run while
Can someone break down this code for me? Aren't boolean values initialized as false by default? Why is the while loop set to run while true but by default the boolean is false? Also if we break, if hasBirthday[d], wouldnt the hasBirthday[d] = true line not run since its breaking out of the loop. Wouldn't the loop never end since we never set anything to false?
public class Birthday ( public static void main(Stringl] args) int days-Integer.parseInt (args[0])/ number of days int people0 total number of people hasBirthday[d] true if someone born on day d; false otherwise /auto-initialized to false boolean[] hasBirthday = new boolean [days]; while (true) people+ int d-(int) (daysMath.random)) I/ integer between 0 and days-1 if (hasBirthday[d]) break; hasBirthday[d] -true; 7two people with the same birthday, so break out of loop update array System.out.println (people)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
