制作一个读写数据的程序——然后计算最小值、最大值、平均值
Making a Program that Reads and Writes Data - Then Computes Min, Max, Average
我正在编写一个程序,从文本文件中读取一些数据,然后获取该数据并找出数字的最小值、最大值和平均值。出于某种原因,我遇到了很多以前从未见过的荒谬错误。这是我的代码:
import java.io.File;
import java.util.Scanner;
import java.io.FileWriter;
public class Lab1 {
static int count = 0;
static int[] newData2 = new int[count];
// Method for reading the data and putting it into different arrays
static int[] readData() {
File f = new File("data.txt");
int[] newData = new int[100];
try {
Scanner s = new Scanner(f);
while (s.hasNext()) {
newData[count++] = s.nextInt();
}
for (int i = 0; i < newData2.length; i++) {
newData[i] = newData2[i];
return newData2;
}
} catch (Exception e) {
System.out.println("Could not read the file.");
}
}
static int min(int[] newData2) {
int min = newData2[0];
for (int i = 0; i < newData2.length; i++) {
if (newData2[i] < min) {
min = newData2[i];
}
}
return min;
}
static int max(int[] newData2) {
int max = newData2[0];
for (int i = 0; i < newData2.length; i++) {
if (newData2[i] > max) {
max = newData2[i];
}
}
return max;
}
static double average(int[] newData2) {
double average = 0;
int sum = 0;
for (int i = 0; i < newData2.length; i++) {
sum = newData2[i];
}
average = sum / newData2.length;
return average;
}
/*
* static int stddev(int[] newData2) { int[] avgDif = new
* int[newData2.length]; for(int i = 0; i < newData2.length; i++) {
* avgDif[i] = (int) (average(newData2) - newData2[i]); }
*
* }
*/
void write(String newdata, int min, int max, double average, int stddev) {
try {
File file = new File("stats.txt");
file.createNewFile();
FileWriter writer = new FileWriter("stats.txt");
writer.write("Minimum: " + min + "Maximum: " + max + "Average: " + average);
writer.close();
}catch(Exception e) {
System.out.println("Unable to write to the file.");
}
public static void main(String[] args) {
}
}
}
我的 readData
方法有一个错误,它告诉我:
This method must return a result type of int[].
我实际上是在返回一个 int 数组,所以我不明白这里的问题是什么。
然后在我的 main 方法中它说 void 是变量 main 的无效类型。
static int[] readData() {
File f = new File("data.txt");
int[] newData = new int[100];
try {
Scanner s = new Scanner(f);
while (s.hasNext()) {
newData[count++] = s.nextInt();
}
for (int i = 0; i < newData2.length; i++) {
newData[i] = newData2[i];
return newData2;
}
} catch (Exception e) {
System.out.println("Could not read the file.");
}
//TODO: return something here if there is some kind of error
}
由于 try-catch 块,您需要考虑可能发生的每一种可能性。当你 return 数组时程序成功时你期望一个 return,但是当出现异常时程序仍然期望一个 return 值,但你没有提供一个。
这里有一些建议:
- 每个方法的出口点 returning something 必须 return something,如果行
new Scanner(f);
抛出异常,第一个 return 没有到达,所以你需要一个默认的,像这样:
private int[] readData() {
File f = new File("data.txt");
int count = 0;
int[] newData = new int[100];
try {
Scanner s = new Scanner(f);
while (s.hasNext()) {
newData[count++] = s.nextInt(); // maybe you should handle the case where your input is too large for the array "newData"
}
return Arrays.copyOf(newData, count);
} catch (Exception e) {
System.out.println("Could not read the file.");
}
return null;
}
- 要减小数组的大小,您应该使用
Arrays.copyOf
(见下文)
- 您应该避免静态字段(在您的情况下 none 是必需的)
- 你的方法
min
和 max
假设数组中有元素(至少一个),你不应该这样做(或用 if 测试它):
private int min(int[] data) {
int min = Integer.MAX_VALUE; // handy constant :)
for (int i = 0; i < data.length; i++) {
if (data[i] < min) {
min = data[i];
}
}
return min;
}
private int max(int[] data) {
int max = 0;
for (int i = 0; i < data.length; i++) {
if (data[i] > max) {
max = data[i];
}
}
return max;
}
- 你的
average
方法有几个错误:
private double average(int[] data) {
int sum = 0;
for (int i = 0; i < data.length; i++) {
sum += data[i]; // here if you want a sum it's += not =
}
return (1.0 * sum) / data.length; // you want a double division, local "average" was useless
}
- 数组是可迭代的,因此您可以使用 "new style" for 循环:
for (int value : newData) {
// use value
}
一些阅读:
- Java Integer division: How do you produce a double?
- “Missing return statement” within if / for / while
我正在编写一个程序,从文本文件中读取一些数据,然后获取该数据并找出数字的最小值、最大值和平均值。出于某种原因,我遇到了很多以前从未见过的荒谬错误。这是我的代码:
import java.io.File;
import java.util.Scanner;
import java.io.FileWriter;
public class Lab1 {
static int count = 0;
static int[] newData2 = new int[count];
// Method for reading the data and putting it into different arrays
static int[] readData() {
File f = new File("data.txt");
int[] newData = new int[100];
try {
Scanner s = new Scanner(f);
while (s.hasNext()) {
newData[count++] = s.nextInt();
}
for (int i = 0; i < newData2.length; i++) {
newData[i] = newData2[i];
return newData2;
}
} catch (Exception e) {
System.out.println("Could not read the file.");
}
}
static int min(int[] newData2) {
int min = newData2[0];
for (int i = 0; i < newData2.length; i++) {
if (newData2[i] < min) {
min = newData2[i];
}
}
return min;
}
static int max(int[] newData2) {
int max = newData2[0];
for (int i = 0; i < newData2.length; i++) {
if (newData2[i] > max) {
max = newData2[i];
}
}
return max;
}
static double average(int[] newData2) {
double average = 0;
int sum = 0;
for (int i = 0; i < newData2.length; i++) {
sum = newData2[i];
}
average = sum / newData2.length;
return average;
}
/*
* static int stddev(int[] newData2) { int[] avgDif = new
* int[newData2.length]; for(int i = 0; i < newData2.length; i++) {
* avgDif[i] = (int) (average(newData2) - newData2[i]); }
*
* }
*/
void write(String newdata, int min, int max, double average, int stddev) {
try {
File file = new File("stats.txt");
file.createNewFile();
FileWriter writer = new FileWriter("stats.txt");
writer.write("Minimum: " + min + "Maximum: " + max + "Average: " + average);
writer.close();
}catch(Exception e) {
System.out.println("Unable to write to the file.");
}
public static void main(String[] args) {
}
}
}
我的 readData
方法有一个错误,它告诉我:
This method must return a result type of int[].
我实际上是在返回一个 int 数组,所以我不明白这里的问题是什么。
然后在我的 main 方法中它说 void 是变量 main 的无效类型。
static int[] readData() {
File f = new File("data.txt");
int[] newData = new int[100];
try {
Scanner s = new Scanner(f);
while (s.hasNext()) {
newData[count++] = s.nextInt();
}
for (int i = 0; i < newData2.length; i++) {
newData[i] = newData2[i];
return newData2;
}
} catch (Exception e) {
System.out.println("Could not read the file.");
}
//TODO: return something here if there is some kind of error
}
由于 try-catch 块,您需要考虑可能发生的每一种可能性。当你 return 数组时程序成功时你期望一个 return,但是当出现异常时程序仍然期望一个 return 值,但你没有提供一个。
这里有一些建议:
- 每个方法的出口点 returning something 必须 return something,如果行
new Scanner(f);
抛出异常,第一个 return 没有到达,所以你需要一个默认的,像这样:
private int[] readData() {
File f = new File("data.txt");
int count = 0;
int[] newData = new int[100];
try {
Scanner s = new Scanner(f);
while (s.hasNext()) {
newData[count++] = s.nextInt(); // maybe you should handle the case where your input is too large for the array "newData"
}
return Arrays.copyOf(newData, count);
} catch (Exception e) {
System.out.println("Could not read the file.");
}
return null;
}
- 要减小数组的大小,您应该使用
Arrays.copyOf
(见下文) - 您应该避免静态字段(在您的情况下 none 是必需的)
- 你的方法
min
和max
假设数组中有元素(至少一个),你不应该这样做(或用 if 测试它):
private int min(int[] data) {
int min = Integer.MAX_VALUE; // handy constant :)
for (int i = 0; i < data.length; i++) {
if (data[i] < min) {
min = data[i];
}
}
return min;
}
private int max(int[] data) {
int max = 0;
for (int i = 0; i < data.length; i++) {
if (data[i] > max) {
max = data[i];
}
}
return max;
}
- 你的
average
方法有几个错误:
private double average(int[] data) {
int sum = 0;
for (int i = 0; i < data.length; i++) {
sum += data[i]; // here if you want a sum it's += not =
}
return (1.0 * sum) / data.length; // you want a double division, local "average" was useless
}
- 数组是可迭代的,因此您可以使用 "new style" for 循环:
for (int value : newData) {
// use value
}
一些阅读:
- Java Integer division: How do you produce a double?
- “Missing return statement” within if / for / while