看起来这是这类问题的一周。在阅读了所有新的和几个旧的之后,我同样感到困惑!
我有一个包含 5 名员工的文本文件,每个员工的姓名下方都列出了 10 个薪水值。我要阅读这个文件,找到并显示每个人的员工姓名、最低工资、最高工资和平均工资。我必须有 3 个循环:一个用于控制读取文件,一个用于将数据加载到数组中,一个用于计算。我必须在一行上打印每个人的信息,而且我必须允许小数点四舍五入到小数点后 2 位,这显然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);
}
}
}