-6

我做了一些谷歌搜索,并没有完全找到我要找的东西。我有一个应用程序,用户在其中输入值,当按下计算时,它将生成 64 个值。我的问题是两部分。

  1. 如何捕获这些结果并创建一个临时数组
  2. 如何从数组中选择最大值并将其分配给 double 以在应用程序的最终方程式中使用。

预先感谢大家的帮助。

4

3 回答 3

1

您应该使用数组列表。它的优点是可排序。默认情况下,数值从低到高排序。因此,只需使用列表的最后一个元素进行计算。但是如何将 64 个预先计算的值放入这个数组列表中取决于您。我建议在每次计算后立即缓冲它。

import java.util.*;
public class Test { 
public static void main(String[] args) {
    ArrayList<Double> list = new ArrayList<Double>();
    for (int i = 0; i < 64; ++i) {
                    //i assume that you use doubles
        list.add(new Double(Math.random()*100));
    }
    Collections.sort(list);
    System.out.println("highest value: " + list.get(63));
}
}
于 2013-03-03T17:55:31.913 回答
0

好吧,首先你需要创建一个数组并用结果填充它:

Double[] results = new Double[64];// (64 is the length of the array, the number of results)

我不知道您如何获得结果,但我认为您将每个结果存储在一个临时变量(double_result)中:

for(int i = 0; i < 64; i++){
    results[i] = double_result;
}

要选择最大的值:

// Create a var with contains the biggest value
double max_value = 0;
// Compare all the values in the array with max_value, if its bigger store the new max value in max_malue
for(int i = 0; i < results.length; i++){
    if(results[i] > max_value){
        max_value = results[i];
    }
}
// Now in *max_value* you got the biggest value of the 64 results
于 2013-03-03T17:37:34.950 回答
0

你在聊天中写道:

这里有3个方程。我需要将 Oytput_0、Output_1、Output_2 添加到数组中,然后获取最高值并将其分配给双精度值,以便我可以在等式中使用它。

    Output_0 = temp1 + (temp2 / 2) - Math.sqrt( (Cvalue_0 * Cvalue_0) - (Avalue_0 * Avalue_0) );
    Output_1 = temp1 + (temp2 / 2) - Math.sqrt( (Cvalue_1 * Cvalue_1) - (Avalue_1 * Avalue_1) );
    Output_2 = temp1 + (temp2 / 2) - Math.sqrt( (Cvalue_2 * Cvalue_2) - (Avalue_2 * Avalue_2) );

好的,试试这样的:

Double[] outputs = new Double[3];
outputs[0] = temp1 + (temp2 / 2) - Math.sqrt( (Cvalue_0 * Cvalue_0) - (Avalue_0 * Avalue_0) );
outputs[1] = temp1 + (temp2 / 2) - Math.sqrt( (Cvalue_1 * Cvalue_1) - (Avalue_1 * Avalue_1) );
outputs[2] = temp1 + (temp2 / 2) - Math.sqrt( (Cvalue_2 * Cvalue_2) - (Avalue_2 * Avalue_2) );

Arrays.sort(outputs);
// Now outputs[2] will have the highest value. Use it however you please.

请注意,我希望这三个方程不是64 个几乎相同的手写方程中的三个。因为如果您将cValue_xs 存储在一个数组中而将aValue_xs 存储在另一个数组中,那么您可以简单地遍历一个方程:

int count = Math.min(cValues.length, aValues.length);
for(int i = 0; i < count; i++) 
    outputs[i] = temp1 + (temp2 / 2) - Math.sqrt( (cValues[i] * cValues[i]) - (aValues[i] * aValues[i]) );
于 2013-03-03T17:43:58.760 回答