gpt4 book ai didi

java - 将不规则文件读入二维数组 - 打印 - 并找到列的平均值

转载 作者:太空宇宙 更新时间:2023-11-04 12:55:48 24 4
gpt4 key购买 nike

我无法读取整个文件并将其放入数组中。它总是遗漏我的 txt 文件中的最后 2 个数字。我在 createArr() 方法中做错了什么导致了这个问题。另外,我的方法 avgTimeTemp() 除了第一行之外不产生任何输出。我在下面包含了我的文本文件内容和输出。

文本文件内容

98 95 95 102

99 96

99.5 97

100 97.5 97.5

101 98.5 98 101 100 102.5

99.5

99.5 95 96.5 102 97.5

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

public class Temperature2 {

public static void main(String[] args) {
// TODO Auto-generated method stub

try {
//create scanner and read file
File input = new File("lab2input2.txt");
Scanner klg = new Scanner(input);

//Variable declarations
double [][] arr = new double [7][];

System.out.println("Hello! Thank you for saving lives! ");
System.out.println(" ");
System.out.println("Please fill in the data for your patient:");
System.out.println("Patient Name:");
System.out.println("Date(Reporting week): ");
System.out.println(" ");
System.out.println("This Weeks temperature data is incomplete and fragmented... but thats ok! We still have your averages: ");
System.out.println("Your Patients Weekly Temperature Record for Time Classes T1 - T6 are as follows:");
System.out.println(" NOTE: Week starts MONDAY(Day1) and ends SUNDAY(Day7)");
System.out.println(" ");


//Method Calls
createArr(klg, arr);
printArr(arr);
avgDailyTemp(arr);
avgTimeTemp(arr);

klg.close();
} //End of Try
catch (FileNotFoundException e) {
System.out.println("The file was not found.");
}
} //End of Main

public static void createArr(Scanner klg, double [][] arr) {
int i, j;

for(i = 0; i < arr.length; i++) {
if(arr.length != 7 && arr != null){
System.out.print("There is not data for 7 days in the file you inputed. Please check file contents.");
}
arr[i] = new double[i]; //I Feel I have a problem in this line
for(j = 0; j< arr[i].length; j++) {
arr[i][j] = klg.nextDouble();
}
}
}
//Print array (TESTING PURPOSES)
public static void printArr(double [][] arr) {
System.out.println("The file contains the following input values: ");
for (int i = 0; i< arr.length; i++){
for(int j = 0; j < arr[i].length; j++) {
System.out.println(arr[i][j] + " ");
}
}
}
//The average temperature of a patient each day from Monday to Sunday
public static void avgDailyTemp(double [][] arr) {
double sum, avg;
System.out.println("Average temperature each day this week: ");
for(int i = 0; i < arr.length; i++) {
sum = 0;
for(int j = 0; j<arr[i].length; j++){
sum = sum + arr[i][j];
}
avg = sum/6;
System.out.println("Day " + (i+1) + " " + avg);
}
System.out.println(" ");
}
//NOT WORKING - The average temperature of patient at Time Classes T1-T6
public static void avgTimeTemp(double [][] arr) {
double sum;
System.out.println("Average temperature for each Time Class: ");

for (int j = 0; j < arr[j].length; j++){
double [] avgArr = new double [j];
sum = 0;
for (int i = 0; i < arr.length; i++){
sum = sum + arr[i][j];
}
avgArr[j] =sum/7;
System.out.println("Time Class T" + (j+1) + ": " + avgArr[j]);
}
System.out.println(" ");
}
} //End of Class

输出:

Hello! Thank you for saving lives! 

Please fill in the data for your patient:
Patient Name:
Date(Reporting week):

This Weeks temperature data is incomplete and fragmented... but thats ok! We still have your averages:
Your Patients Weekly Temperature Record for Time Classes T1 - T6 are as follows:
NOTE: Week starts MONDAY(Day1) and ends SUNDAY(Day7)

The file contains the following input values:

98.0

95.0

95.0

102.0

99.0

96.0

99.5

97.0

100.0

97.5

97.5

101.0

98.5

98.0

101.0

100.0

102.5

99.5

99.5

95.0

96.5

Average temperature each day this week:

Day 1 0.0

Day 2 16.333333333333332

Day 3 31.666666666666668

Day 4 49.5

Day 5 65.66666666666667

Day 6 82.66666666666667

Day 7 98.83333333333333

Average temperature for each Time Class:

最佳答案

尝试

public static void createArr(Scanner klg, double [][] arr) {

int i = 0;


while (klg.hasNextLine()) {


String line = klg.nextLine();
String [] vals = line.split(" ");

arr[i] = new double [vals.length];
int j = 0;

for (String val : vals) {
arr[i][j++] = Double.parseDouble(val);
}

i++;
}

}

关于java - 将不规则文件读入二维数组 - 打印 - 并找到列的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35403527/

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