+ 1

light bulb java

n People are waiting to enter a room with numbered switches from 1 through n. The first one who enters the room picks up all the switches and all the lights are on. The second lowers each second switch and thus turns off all double-light bulbs. The third changes the status of all the switches that divide by 3 (if the light is on it turns off or turns off - it turns on) The fourth changes all the switches that divide by 4 and so on. What bulbs remained in the room after the last man left the room I need to get two numbers from the user and run the algorithm described and print the same number of lighted bulbs on the same line. Sample output enter number of light bulbs - 16 enter number of people - 3 1 5 6 7 11 12 13 thank you my code is like that but i dont know what to do i do diffrent class that i created to take the bulbs -like scanner.. int people; int bulbs; bulbs= ("Please enter a bulbs: "); people= ("Please enter a people of rows : "); boolean [] room=new boolean[bulbs]; for(int i=1;i<=people;i++) { boolean counter; for(int j=1;j<bulbs;j++) { if(i%j==0){ if(i==1) { room[j]=true;} if(i>1 && room[j]==true) { room[j]=false; counter=room[j]; } else if (room[j]=false) { room[j]=true; counter=room[j]; } } if (counter=true) { System.out.println("Light bulb " + j + " will be on"); } } }

7th Apr 2019, 4:41 PM
Liroy Levi
Liroy Levi - avatar
1 Réponse