Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 让数组以多种方式工作?_Java_Arrays - Fatal编程技术网

Java 让数组以多种方式工作?

Java 让数组以多种方式工作?,java,arrays,Java,Arrays,我目前在学校做一个项目,从.txt文件中读取整数并将其输入数组。这些整数表示一个团队一年生产的产品数量。然后我要计算平均生产率,最高生产率等等 到目前为止,一切进展顺利,我知道如何做到这一点,我唯一不确定的是如何使数组在其他方法中工作,以便我可以计算平均值。这是我到目前为止掌握的代码。有一些关于菜单的评论我还没有编写,但希望你能看到我想做什么,如果用户想看到平均、最高或最低的生产效率,给他们一个选项。被扫描的文件实际上只是一个.txt文件,每个月的第1行都有一个数字 /** * @author

我目前在学校做一个项目,从.txt文件中读取整数并将其输入数组。这些整数表示一个团队一年生产的产品数量。然后我要计算平均生产率,最高生产率等等

到目前为止,一切进展顺利,我知道如何做到这一点,我唯一不确定的是如何使数组在其他方法中工作,以便我可以计算平均值。这是我到目前为止掌握的代码。有一些关于菜单的评论我还没有编写,但希望你能看到我想做什么,如果用户想看到平均、最高或最低的生产效率,给他们一个选项。被扫描的文件实际上只是一个.txt文件,每个月的第1行都有一个数字

/**
 * @author  Jack Orr
 * @version 1.0
 * @since   10-12-2014 
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;

// Begin by importing the utilities allowing for scanners and reading from files

public class Assignment {

    // main method which will call the other methods, employ a switch to choose
    // methods

    public static void main(String args[]) throws FileNotFoundException {

        readStats2012();
        readStats2013();
    }

    public static void readStats2012() throws FileNotFoundException {

        // This method reads the values from a keyboard scanner and assigns them to an array

        System.out.println("Productivity for 2012\n");
        ArrayList<Integer> teams = new ArrayList<Integer>();
        int inTeam;

        Scanner in = new Scanner(new File("in2012.txt"));
        while (in.hasNextInt()) {
            inTeam = in.nextInt();
            teams.add(inTeam);
        }

        in.close();

        for (int i = 0; i < teams.size(); i++) {
            System.out.println("Team " + (i + 1) + "\t" + teams.get(i));
        }
    }

    public static void readStats2013() throws FileNotFoundException {

        // This method reads the values from a keyboard scanner and assigns them to an array

        System.out.println("\nProductivity for 2013\n");
        ArrayList<Integer> teams = new ArrayList<Integer>();
        int inTeam;

        Scanner in = new Scanner(new File("in2013.txt"));
        while (in.hasNextInt()) {
            inTeam = in.nextInt();
            teams.add(inTeam);
        }

        in.close();

        for (int i = 0; i < teams.size(); i++) {
            System.out.println("Team " + (i + 1) + "\t" + teams.get(i));
        }   

    }
    static void calculateandDisplay(){

        // This will be a menu option to calculate the average, the highest and the lowest

        }

    public static double calculateAverage(){

        double average = 0;
        if(i = 0; i < teams.length; i++){

        }
        return average;

    }
}

我建议您返回列表并将其保存为局部变量,并将这些变量作为参数传递给要调用的方法。

我建议您将两个read方法重构为一个参数化方法:

public static void readStats(int year) throws FileNotFoundException {
    ...
    Scanner in = new Scanner(new File("in" + year + ".txt"));
    ...
}
然后使用List类型的静态字段保存所有数据:

static List<List<Integer>> years = new ArrayList<List<Integer>>();

首先,考虑使用一个将文件名字符串作为参数而不是具有相同代码来读取特定年份数据的单一方法。下一年、下一年或十年后会发生什么;为每个可能的年份编写重复的方法是否合理?这是一个很好的观点,您的建议更实用!
years.add(teams);