java - I need help on boolean arrays -
i'm stuck on array problem , use help.
the first visitor occupy middle position:
_ _ _ _ _ x _ _ _ _
the next visitor in middle of empty area on left side, longest sequence of unoccupied places:
_ _ x _ _ x _ _ _ _ etc...
i have made start , need continuing. here's code , please me. have boolean array created cant figure out how print out contents of or , see know elements true or false can fill out rest of array.
import java.util.scanner; public class homework06 { public static void main(string[] args) { scanner keyboard = new scanner (system.in); int answer = 0; string start=""; answer=promptfornumberofstalls(); int[] stalls=new int[answer]; boolean firstperson[]=new boolean[answer]; start=firstperson(answer); occupyonemore(answer, start, firstperson); } public static int promptfornumberofstalls (){ int answer =0; scanner keyboard = new scanner (system.in); system.out.println("hello, how many stalls there be"); answer = keyboard.nextint(); return answer; } public static string firstperson( int answer){ boolean[] firstperson = new boolean[answer]; string start=""; for(int i=0;i<firstperson.length;i++){ if(firstperson[i]) start+="x"; else start+="_"; } return start; } public static boolean[] occupyonemore(int answer, string start, boolean[] firstperson){ int middle=answer/2+1; firstperson[answer/2+1]=true; (int = 0; < firstperson.length; i++) { if (firstperson[i] == true) { middle = (int) math.floor(i / 2); // use math.ceil round system.out.println(middle); if(middle==i){ firstperson[i]=true; } } } system.out.println(arrays.tostring(firstperson)); return firstperson; }
}
to print contents of array, use arrays.tostring()
:
boolean[] ba = new boolean[]{true,true,false,true}; system.out.println(arrays.tostring(ba));
will produce output
[true,true,false,true]
Comments
Post a Comment