【问题标题】:i need to read from a text file save to an array and then generate a report我需要从文本文件中读取保存到数组,然后生成报告
【发布时间】:2017-04-08 17:02:07
【问题描述】:

创建一个循环,直到乘客数组为空。在此循环中,您必须: 生成一个从 1 到 6 的随机数。将与您刚刚生成的人数相等的乘客数量添加到您在 Airport 类中的队列中。您需要从阵列中带走这些乘客。 我无法将数组中的元素添加到队列中。这是我尝试过的

  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));

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    你应该使用:

    int k = new Random().nextInt(6) + 1;
    

    因为 Random().nextInt(n) 给你一个随机的 [0,n[.

    在您发表评论后,我会建议您替换为:

    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);
    }
    

    【讨论】:

    • 但是我如何将这个数量的乘客添加到队列中?这个生成的数字必须是从数组中取出到队列中的乘客人数
    • @lele 我重写了答案以更好地回答您的评论。
    • addqueue(passengers[i]) 出错;它说字符串不能转换为乘客
    • @lele 您没有显示太多代码。也许你的队列是乘客?队列?我假设您使用的是原始代码中的 addqueue(String s)。
    • 乘客[]乘客=新乘客[MaxStay];这是我的队列
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多