我写了一个程序,它从一个有数百组数字的文件中读取(每组有20个数字)。 程序的要点是产生每组的总和,#even,#of odd。
这是我的代码:
public static void main(String args[])
{
System.out.println(String.format("%-10s%-10s%-10s", "sum", "even", "odd"));
try
{
FileInputStream fstream = new FileInputStream("data.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] numberStrs = strLine.split("\t");
int[] numbers = new int[numberStrs.length];
for(int i = 0;i < numberStrs.length;i++)
{
numbers[i] = Integer.parseInt(numberStrs[i]);
//System.out.println(numbers[i]);
}
int sumTotal = sum(numbers);
for (int j =0; j<numbers.length;j++)
{
if (isEven(numbers[j]) == true)
{
evenCounter++;
}
else
{
oddCounter++;
}
}
System.out.println(String.format("%-10s%-10s%-10s", sumTotal, evenCounter, oddCounter + "\n");
}
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
现在,输出是数百行和3列(总和,偶数,奇数)。 我的问题是,如何按总和对输出进行排序(偶数和奇数与总和相关联。)
例如:
sum odd even
404 4 10
120 21 5
我想要的是对总和进行排序并具有:
sum odd even
120 21 5
404 4 10
答案 0 :(得分:1)
您的数据需要存储,以便以后进行排序。最好的方法是设计一个负责的类:
这会从代码中移除一些复杂性。这是一个例子。
Sum类:
public class Sum implements Comparable<Sum>
{
private int sumTotal;
private int evenCount;
private int oddCount;
public void addNumber(int n)
{
sumTotal += n;
if ((n & 0x1) == 0x1)
{
oddCount++;
}
else
{
evenCount++;
}
}
@Override
public int compareTo(Sum other)
{
return Integer.compare(sumTotal, other.sumTotal);
}
@Override
public String toString()
{
return String.format("%-10s%-10s%-10s", sumTotal, evenCount, oddCount);
}
}
您的修改后的代码:
public static void main(String[] args)
{
System.out.println(String.format("%-10s%-10s%-10s", "sum", "even",
"odd"));
try
{
// The maintained sum list
List<Sum> sumList = new ArrayList<Sum>();
InputStream fstream = new FileInputStream("data.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null)
{
// Prepare to insert a new sum object
Sum newSum = new Sum();
String[] numberStrs = strLine.split("\t");
for (String numberStr : numberStrs)
{
// The sum object manages its own tallies
newSum.addNumber(Integer.parseInt(numberStr));
}
// Append the new object to the list
sumList.add(newSum);
}
in.close();
// Sort the list
Collections.sort(sumList);
// Print the list
for (Sum sum : sumList)
{
/*
* The sum object is responsible for generating its own string
* representation
*/
System.out.println(sum);
}
}
catch (Exception e)
{
// Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
答案 1 :(得分:0)
为了排序,您必须将计算出的数据存储在某处,而不是动态打印。 您可以将它们存储在一个3 x N数组中,编写一个Comparator,它只比较它的一个维度,然后用比较器对数组进行排序。
例如:
int[][] arr = new int[N][3];
// populate arr with data here
Arrays.sort(arr, new Comparator<int[]>(){
@Override
public int compare(int[] o1, int[] o2) {
return o1[0] - o2[0];
}
});
答案 2 :(得分:0)
您可以拥有一个Set类,您可以在其中保存总和,赔率数和平均数。保存一组Set,然后你可以用sum对它进行排序(你可以编写一个简单的Comparator),然后显示它。