java - Using 1 array to implement 2 for loop programs -
i trying make program every count of 2 changes value "open" or "1".
1-2-3-4-5-6-7-8-9-10
0-1-0-1-0-1-0-1-0-1 ** ** , after in every 3 count changes "open" or "1".
but if box "open" or "1". should "close" or "0".
and on 150 count.
my problem cant make output show 1 array.i have no problem doing if different array..help...
int[] box = new int [150]; int b = 0; (a = 1; < box.length; a++) { if (a % 2 == 0) { box[b] = 1; } else { box[b] = 0; } } (int c = 1; c < box.length; c++) { if (c % 3 == 0) { if (box[b] == 1) { box[b] = 0; } else { box[b] = 1; } } else { box[b] = 0; } system.out.print(box[b]); //putting result here result in 001001001; } /*system.out.print(box[b]); -- putting outside results in zero.
how can combine 2 for-loops 1 array? hope makes sense of you. different approach okay long loop , array used. output program should 011101011101 when 2 together...
this 2nd program did please check cause suppose add "0"'s , "1"'s after program executed want know how add it.putting inside loop loop 150 times
int box [] = new int [150]; int ones= 0; int zero= 0; for(int a= 0;a<box.length;a = a+2){ box[a] = 1; for(int b =0;b<box.length;b = b+3){ if(box[b]==1){ box[b] = 0; zero++; } else{ box[b] = 1; one++; } system.out.print("no. of ones : " + one); /* putting here loops additions 150 , resulting thousands of ones */ } system.out.print(no. of ones: " + one); /* putting here result 0 , loops 150 counting thousands of 0 */ } for(int =0; < box.length; i++){ system.out.print(box[i]); resulting 00111 - 150 /* next count 4 enough loops add 5 next , add 6 7 , 150 counts till hitting "open" or "1"? system.out.print("no. of one: " + sum); } -- putting here result 0
thanks hope understand questions (theres lot of them)and i'm confused
you iterating on a
, later on c
assigning box[b]
. , b
remains 0
, keep assigning box[0]
. should assign box[a]
, box[c]
.
in addition, should start loops 0, not 1.
Comments
Post a Comment