看起来本周是此类问题的一周。在读完所有新书和几本旧书后,我的困惑也没有减少!
我有一个包含 5 名员工的文本文件,每个员工的名字下方都列出了 10 个薪资值。我要读取这个文件,找到并显示每个人的员工姓名、最低工资、最高工资和平均工资。我必须有 3 个循环:一个用于控制读取文件,一个用于将数据加载到数组中,一个用于进行计算。我必须在一行上打印每个人的信息,并且我必须允许小数四舍五入到小数点后两位,显然使用我从未听说过的 Math.round
!
我很尴尬地向您展示我拥有的困惑代码,因为它并不多,但在阅读完我拥有的所有代码后,我不知道我是否已经正确开始。我不知道我是否有正确的想法如何继续。感谢您的帮助。
更新代码:再次!
import javax.swing.*;
import java.io.*;
public class MinMaxSalary3
{
public static void main(String args[])throws Exception
{
// Declare input file to be opened.
FileReader fr = new FileReader ("salary.dat");
BufferedReader br = new BufferedReader (fr);
//General Declarations
final String TITLE = "Employee's Salary Report";
String employeeName, salaryString;
double avgSalary=0.0;
double totalSalary = 0.0;
double sum = 0.0;
// Declare Named Constant for Array.
final int MAX_SAL = 10;
// Declare array here.
int salary[] = new int[MAX_SAL];
System.out.println (TITLE);
while ((employeeName = br.readLine()) != null)
{
System.out.print ("" + employeeName);
// Use this integer variable as your loop index.
int loopIndex;
// Assign the first element in the array to be the minimum and the maximum.
double minSalary = salary[1];
double maxSalary = salary[1];
// Start out your total with the value of the first element in the array.
sum = salary[1];
// Write a loop here to access array values starting with number[1]
for (loopIndex = 1; loopIndex < MAX_SAL ;loopIndex++)
// Within the loop test for minimum and maximum salaries.
{
if (salary[loopIndex] < minSalary)
{
minSalary = salary[loopIndex];
if (salary[loopIndex] > maxSalary)
maxSalary = salary[loopIndex];
}
{
// Also accumulate a total of all salaries.
sum += sum;
// Calculate the average of the 10 salaries.
avgSalary = sum/MAX_SAL;
}
// I know I need to close the files, and end the while loop and any other loops. I just can't think that far right now.
}
{
// Print the maximum salary, minimum salary, and average salary.
System.out.println ("Max Salary" + maxSalary);
System.out.println ("Min Salary" + minSalary);
System.out.println ("Avg Salary" + avgSalary);
}
System.exit(0);
}
}
}
最佳答案
I must have 3 loops: One to control reading the file, one to lad the data into the array, and one to do the calculations.
我在下面写的内容现在对您来说可能更加晦涩难懂,但如果您通过了这门类(class),了解这些内容可能会很有用。
另一种看待这个问题的方法是更加面向对象和更好的分解:您需要一个对象来保存数据、执行计算并呈现输出。如何获取这些数据并不重要。今天是文件;下次可能是 HTTP 请求。
从 Employee 对象开始。我故意遗漏了很多你必须填写并弄清楚的细节:
package model;
public class Employee {
private String name;
private double [] salaries;
public Employee(String name, int numSalaries) {
this.name = name;
this.salaries = new double[numSalaries];
}
public double getMinSalary() {
double minSalary = Double.MAX_VALUE;
// you fill this in.
return minSalary;
};
public double getMaxSalary() {
double maxSalary = Double.MIN_VALUE;
// you fill this in.
return maxSalary;
}
public double getAveSalary() {
public aveSalary = 0.0;
if (this.salaries.length > 0) {
// you fill this in.
}
return aveSalary;
}
}
这种方法的优点在于您可以单独测试它,而不必担心有关文件 I/O 的所有废话。正确处理这个物体,把它放在一边,然后处理下一个物体。最终,当您将所有这些较小的部分组装在一起时,您将获得一个干净的解决方案。
使用 JUnit 在没有文件 I/O 的情况下进行测试:
package model;
public class EmployeeTest {
@Test
public void testGetters() {
double [] salaries = { 10000.0, 20000.0, 30000.0, 40000.0 };
Employee testEmployee = new Employee("John Q. Test", salaries);
Assert.assertEquals("John Q. Test", testEmployee.getName());
Assert.assertEquals(10000.0, testEmployee.getMinSalary(), 1.0e-3);
Assert.assertEquals(40000.0, testEmployee.getMaxSalary(), 1.0e-3);
Assert.assertEquals(25000.0, testEmployee.getMinSalary(), 1.0e-3);
}
}
关于java - 将txt文件读入数组,然后找到工资的最小值、最大值和平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12999675/