[英]i need to read from a text file save to an array and then generate a report
Create a loop that works till the array of passengers is empty. 创建一个循环,直到乘客排空为止。 Inside this loop you have to: Generate a random number from 1 to 6.Add to the Queue, which you have in your Airport class, a number of passengers equal to the number that you just generated. 在此循环内,您必须:生成1到6之间的随机数。将您的乘客人数等于您刚刚生成的人数添加到您的Airport类中的队列中。 You need to take these passengers from the array. 您需要从阵列中带走这些乘客。 I cant get it to add elements from the array to the queue . 我无法获取将数组中的元素添加到队列中的信息。 Here is what i have tried 这是我尝试过的
void runningStimulation() throws FileNotFoundException, IOException {
String Line = "";
Scanner inFile1 = new Scanner(new BufferedReader(new FileReader("passengers.dat")));
List<String> temps = new ArrayList<String>();
while (inFile1.hasNext()) {
Line = inFile1.nextLine();
temps.add(Line);
}
inFile1.close();
String[] passengers = temps.toArray(new String[0]);
for (String s : passengers) {
System.out.println(s);
int Random = new Random().nextInt(6);
int k = Random;
addqueue(passengers(k));
You should use: 您应该使用:
int k = new Random().nextInt(6) + 1;
because Random().nextInt(n) gives you a random in [0,n[. 因为Random()。nextInt(n)为您提供了[0,n [。
after your comment i would suggest this to replace your for: 在您发表评论后,我建议您将其替换为:
int rng = new Random().nextInt(6) + 1;
for(int i = 0; i < rng; i++){
Passenger p = new Passenger(passenger[i] /* TODO: default_constructor, assuming that the argument for Passenger constructor is String */);
addqueue(p);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.