gpt4 book ai didi

java - 我正在尝试创建一种方法来计算两个数组中数字的平均值

转载 作者:行者123 更新时间:2023-12-01 11:39:13 25 4
gpt4 key购买 nike

import java.io.*;
import java.util.*;


public class Marks {
public static void main(String[] args) {
Marks r = new Marks();
double[] finalArray = r.openFile();
double[] finalArray2 = r.openFile2();


}
//ID's and first set of grades

private Scanner a;

public double[] openFile() {
ArrayList<String> list1 = new ArrayList<String>(7);
try {
a = new Scanner(new File("IR101.txt"));
} catch (Exception e) {
System.out.println("could not find file");
}
while (a.hasNextLine()) {
list1.add(a.nextLine());

}

String[] arrayOne = list1.toArray(new String[list1.size()]);
Arrays.sort(arrayOne);
//System.out.println(Arrays.toString(arrayOne));
int size = arrayOne.length;
double[] finalArray = new double[size];
for (int j = 0; j < size; j++) {
String word = arrayOne[j];
String newWord = word.substring(6, 10);
double grade = Double.parseDouble(newWord);
finalArray[j] = grade;

}return finalArray;



}
//ID's and second set of grades


private Scanner b;

public double[] openFile2() {
ArrayList<String> list2 = new ArrayList<String>(7);
try {
b = new Scanner(new File("IR102.txt"));
} catch (Exception e) {
System.out.println("could not find file");
}
while (b.hasNextLine()) {
list2.add(b.nextLine());
}
String[] arrayTwo = list2.toArray(new String[list2.size()]);
Arrays.sort(arrayTwo);
//System.out.println(Arrays.toString(arrayTwo));
int size = arrayTwo.length;
double[] finalArray2 = new double[size];
for (int j = 0; j < size; j++) {
String word = arrayTwo[j];
String newWord = word.substring(6, 10);
double grade2 = Double.parseDouble(newWord);
finalArray2[j] = grade2;
}return finalArray2;

}



// ID's and names

private Scanner c;

public void openFile3() {
ArrayList<String> list3 = new ArrayList<String>(7);
try {
c = new Scanner(new File("IRStudents.txt"));
} catch (Exception e) {
System.out.println("could not find file");
}
while (c.hasNextLine()) {
list3.add(c.nextLine());
}
String[] arrayThree = list3.toArray(new String[list3.size()]);
Arrays.sort(arrayThree);
//System.out.println(Arrays.toString(arrayThree));
int size = arrayThree.length;

String[] names = new String[size];
for (int j = 0; j < size; j++) {
names[j] = arrayThree[j].substring(6);
}
String[] IDs = new String[size];
for (int x = 0; x < size; x++){
IDs[x] = arrayThree[x].substring(0,5);
}
System.out.println(Arrays.toString(names));
System.out.println(Arrays.toString(IDs));

}
public void calculateAvg() {


}

}

我正在尝试访问 FinalArray 和 FinalArray2 中的数字,但当我尝试调用这两个数组时,我不确定如何执行此操作,但它不起作用,我认为这与数组的范围有关。所以如何使整个程序可以访问这两个数组。

最佳答案

如果您想让整个程序可以访问这两个数组,请将它们声明为 main 方法外部的静态变量,并在内部进行初始化,如下所示:

    public class Marks {
static double[] finalArray;
static double[] finalArray2;
public static void main(String[] args) {
Marks r = new Marks();
finalArray = r.openFile();
finalArray2 = r.openFile2();

关于java - 我正在尝试创建一种方法来计算两个数组中数字的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29681011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com