gpt4 book ai didi

java - 比较特定索引时数组循环不起作用

转载 作者:行者123 更新时间:2023-12-01 15:38:21 24 4
gpt4 key购买 nike

我正在尝试将两个数组进行比较,以使用其他数组中的相同索引进行一些基本数学计算。我没有收到正确的答案,我认为这是因为数组没有正确比较。这是我的代码:

    y = 0;
for (x=0; x<std1.length; x++){
if (std1[x]==std2[y]){
sumhrs[x] += hrs[y];
mult[x] = hrs[y] * ngrd[y];
hold[x] = hold[x] + mult[x];
gpa[x] = hold[x] / sumhrs[x];
y++;
}
}

数组 std1 包含 4 个数字记录,而数组 std2 包含 9 个数字记录,其中有多个(重复)与 std1 相同的记录。其他数组(hrs、ngrd 等)链接到同一变量 [y],因为它们包含按顺序排列到数组 std2 的记录。

    Here is the current output:    205   Smart   4.0   A    400   Brent   0.0   NONE    155   Brown   0.0   NONE    150   Canon   0.0   NONE    
This is homework and I have spent many hours trying to figure this out and I cannot. The end result of this homework is to calculate the GPA from two student files with name and grade information. I added the files into arrays and then split the arrays into unique arrays with specific format (int, double, string) but kept all duplicates to maintain index accuracy while calculating the GPA. In other words, array std2[5] and hrs[5] kept the same data that they had when they were one combined array at index [5].

Any help would be appreciated.

Thanks

***Edit:

I'm going to add my full code and the two files that I'm pulling from here:

    File 1tab delimited    205 Smart    400 Brent    155 Brown    150 Canon    
    File 2tab delimited    205 1   4.0    205 3   2.8    205 4   4.0    205 3   2.3    400 3   3.5    155 2   2.7    150 3   3.0    150 3   4.0    150 3   2.7    

public static void main(String[] args) throws IOException {
FileInputStream in1 = new FileInputStream("c:/users/da/desktop/"
+ "file1.txt");
BufferedReader br1 = new BufferedReader(new InputStreamReader(in1));
FileInputStream in2 = new FileInputStream("c:/users/da/desktop/file2.txt");
BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));

String[] file1array = new String[4];
String[] file2array = new String[9];

for (int j = 0; j < file1array.length; j++){
file1array[j] = br1.readLine();}
for (int j = 0; j < file2array.length; j++){
file2array[j] = br2.readLine();}

int i = 0;
int j = 0;
int x = 0;
int y = 0;
int index = 00;

int[] std1 = new int [file1array.length];
String[] name = new String [file1array.length];

int[] std2 = new int [file2array.length];
int[] hrs = new int [file2array.length];
double[] ngrd = new double [file2array.length];
double[] mult = new double [file2array.length];
double[] sumhrs = new double [file1array.length];
double[] hold = new double [file2array.length];

double[] gpa = new double [file1array.length];
String[] lgrd = new String [file1array.length];

String line1 = "";
String[] data1;
String line2 = "";
String[] data2;

for (x=0; x<file1array.length; x++){
line1 = file1array[x];
data1 = line1.split("\t");
std1[x] = Integer.parseInt(data1[0]);
name[x] = data1[1];}

for (x=0; x<file2array.length; x++){
line2 = file2array[x];
data2 = line2.split("\t");
std2[x] = Integer.parseInt(data2[0]);
hrs[x] = Integer.parseInt(data2[1]);
ngrd[x] = Double.parseDouble(data2[2]);}

y = 0;
for (x=0; x<std1.length; x++){
if (std1[x]==std2[y]){
sumhrs[x] += hrs[y];
mult[x] = hrs[y] * ngrd[y];
hold[x] = hold[x] + mult[x];
gpa[x] = hold[x] / sumhrs[x];
y++;
}
}

for (x=0; x<gpa.length; x++){
if (gpa[x] <= 4.0){
lgrd[x] = "A";}
if (gpa[x] < 3.4){
lgrd[x] = "B";}
if (gpa[x] < 3.7){
lgrd[x] = "C";}
if (gpa[x] < 2.0){
lgrd[x] = "D";}
if (gpa[x] < 1.3){
lgrd[x] = "F";}
if (gpa[x] < 0.1){
lgrd[x] = "NONE";}
}

System.out.println (" STUDENT REPORT");
System.out.println ("");
System.out.println ("STUDENT STUDENT GPA GRADE");
System.out.println (" NUMBER NAME");
for (x=0; x<gpa.length; x++)
System.out.println (" " + std1[x] + " " + name[x] + " " + gpa[x] +
" " +lgrd[x]);
}

}

最佳答案

我猜测 y++ 应该位于 if 语句之外,而不是在其中。

就目前而言,x 每次通过 for 循环都会递增,但 y 仅在 std1[x] == std2[y]

时才会递增

关于java - 比较特定索引时数组循环不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8499401/

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