出于某种原因,当我尝试打印一个(11.3)时,此代码为数组中的最高值打印三个值.有人可以向我解释为什么这样做吗?
谢谢.
import java.util.Scanner;
public class Slide24
{
public static void main (String [] args)
{
Scanner in = new Scanner(System.in);
double[] decMax = {-2.8, -8.8, 2.3, 7.9, 4.1, -1.4, 11.3, 10.4,
8.9, 8.1, 5.8, 5.9, 7.8, 4.9, 5.7, -0.9, -0.4, 7.3, 8.3, 6.5, 9.2,
3.5, 3, 1.1, 6.5, 5.1, -1.2, -5.1, 2, 5.2, 2.1};
double total = 0, avgMax = 0;
for (int counter = 0; counter < decMax.length; counter++)
{
total += decMax[counter];
}
avgMax = total / decMax.length;
System.out.printf("%s %2.2f\n", "The average maximum temperature for December was: ", avgMax);
//finds the highest value
double max = decMax[0];
for (int counter = 1; counter < decMax.length; counter++)
{
if (decMax[counter] > max)
{
max = decMax[counter];
System.out.println("The highest maximum for the December is: " + max);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Toj*_*oji 37
每当它找到一个高于当前最大值的数字时打印出一个数字(在你的情况下恰好发生三次.)将打印件移到for循环之外你应该是好的.
for (int counter = 1; counter < decMax.length; counter++)
{
if (decMax[counter] > max)
{
max = decMax[counter];
}
}
System.out.println("The highest maximum for the December is: " + max);
Run Code Online (Sandbox Code Playgroud)
小智 24
要从数组中找到最高(最大)或最低(最小)值,这可以为您提供正确的方向.以下是从基本数组中获取最高值的示例代码.
public int maxValue(int array[]){
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < array.length; i++) {
list.add(array[i]);
}
return Collections.max(list);
Run Code Online (Sandbox Code Playgroud)
}
要获得最低值,您可以使用
Collections.min(list)
public int maxValue(int array[]){
int max = Arrays.stream(array).max().getAsInt();
return max;
}
Run Code Online (Sandbox Code Playgroud)
现在,以下行应该有效.
System.out.println("The highest maximum for the December is: " + maxValue(decMax));
扫描完所有内容后,您需要打印出最大值:
for (int counter = 1; counter < decMax.length; counter++)
{
if (decMax[counter] > max)
{
max = decMax[counter];
// not here: System.out.println("The highest maximum for the December is: " + max);
}
}
System.out.println("The highest maximum for the December is: " + max);
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您正在寻找针对数组执行各种操作的最快,最简单的方法,则使用Collections类非常有帮助(可从https://docs.oracle.com/javase/7/docs/api/获得文档。java / util / Collections.html),操作范围从查找最大值,最小值,排序,倒序等。
使用Collections从数组中找到最大值的简单方法:
Double[] decMax = {-2.8, -8.8, 2.3, 7.9, 4.1, -1.4, 11.3, 10.4, 8.9, 8.1, 5.8, 5.9, 7.8, 4.9, 5.7, -0.9, -0.4, 7.3, 8.3, 6.5, 9.2, 3.5, 3.0, 1.1, 6.5, 5.1, -1.2, -5.1, 2.0, 5.2, 2.1};
List<Double> a = new ArrayList<Double>(Arrays.asList(decMax));
System.out.println("The highest maximum for the December is: " + Collections.max(a));
Run Code Online (Sandbox Code Playgroud)
如果您有兴趣寻找最小值,类似于寻找最大值:
System.out.println(Collections.min(a));
Run Code Online (Sandbox Code Playgroud)
对列表进行排序的最简单的行:
Collections.sort(a);
Run Code Online (Sandbox Code Playgroud)
或者,可以使用Arrays类对数组进行排序:
Arrays.sort(decMax);
Run Code Online (Sandbox Code Playgroud)
但是,Arrays类没有直接引用最大值的方法,将其排序并引用最后一个索引是最大值,但是请记住,通过以上两种方法进行排序的复杂度为O(n log n) 。
与其他人建议的相同,只是提到更清洁的方式:
int max = decMax[0];
for(int i=1;i<decMax.length;i++)
max = Math.max(decMax[i],max);
System.out.println("The Maximum value is : " + max);
Run Code Online (Sandbox Code Playgroud)