java - Quick Program help (quick fix) -
i have homework assignment need make array of lightbulb objects. add method "turn them on". need have nested loop have imaginary person turn on every bulb pull string on every other bulb every 3rd , on until every 20 bulbs. code have. compiles when run it, goes forever. please help
public class lightbulb { public boolean isturnedon; public lightbulb() { isturnedon = false; } public boolean ison() { if(isturnedon==false) return false; return true; } public void pullstring() { if(isturnedon==true){ isturnedon=false; } isturnedon=true; } } public class lightdriver { public static void main(string[]arg) { int numon=0; lightbulb[]bulb=new lightbulb[100]; for(int a=0;a<100;a++){ bulb[a]=new lightbulb(); } for(int b=0;b<=19;b++){ for(int c=0;c<=100;c=b+1){ bulb[c].pullstring(); } } for(int d=0;d<100;d++){ if(bulb[d].isturnedon==true){ numon++; } } system.out.println(numon+" lightbulbs on"); } }
this causing problem:
for(int b=0;b<=19;b++){ for(int c=0;c<=100;c=b+1){ bulb[c].pullstring(); } }
for every iteration of inner for
loop, setting c = b + 1
means c
not changing, since b
not changing.
i think want this:
for(int b=1; b <= 20; b++){ for(int c=0; c < 100; c = c + b){ bulb[c].pullstring(); } }
also, pullstring method sets isturnedon true no matter was. think want instead:
public void pullstring() { if (isturnedon) { isturnedon = false; } else { isturnedon = true; } }
Comments
Post a Comment