我被困在阵列问题上,可以使用一些帮助。
第一个访客将占据中间位置:
_ _ _ _ _ X _ _ _ _
下一位访客将在左侧空白区域的中间,因为这是最长的未占用位置序列:
_ _ X _ _ X _ _ _ _
etc...
我有一个良好的开端,需要帮助继续。这是我的代码,请帮助我。我已经创建了布尔数组,但我不知道如何打印出它的内容或查看它,所以我知道哪些元素是真或假,所以我可以填写数组的其余部分。
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 will 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;
for (int i = 0; i < firstPerson.length; i++) {
if (firstPerson[i] == true) {
middle = (int) Math.floor(i / 2); // use Math.ceil to round up
System.out.println(middle);
if(middle==i){
firstPerson[i]=true;
}
}
}
System.out.println(Arrays.toString(firstPerson));
return firstPerson;
}
}