查找数组中最大值的Java程序正在打印多个值
由于某种原因,当我尝试仅打印一个值(即 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);
}
}
}
}
For some reason this code is printing three values for the highest value in the array when I'm trying to print just one (which is 11.3). Can someone please explain to me why it is doing this?
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);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
每当它找到一个高于当前最大值的数字时,它就会打印出一个数字(在您的情况下,这种情况发生了三次。)将打印移到 for 循环之外,您应该会很好。
It's printing out a number every time it finds one that is higher than the current max (which happens to occur three times in your case.) Move the print outside of the for loop and you should be good.
要从数组中查找最高(最大值)或最低(最小值)值,这可以为您提供正确的方向。以下是从原始数组中获取最高值的示例代码。
方法1:
要获得最低值,您可以使用
方法 2:
现在下面的行应该可以工作。
To find the highest (max) or lowest (min) value from an array, this could give you the right direction. Here is an example code for getting the highest value from a primitive array.
Method 1:
To get the lowest value, you can use
Method 2:
Now the following line should work.
扫描完所有内容后,您需要打印出最大值:
You need to print out the max after you've scanned all of them:
如果您正在寻找最快、最简单的方法来执行有关数组的各种操作,那么使用 Collections 类非常有帮助(文档可从 https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html),操作范围包括查找最大值、最小值、排序、倒序等。
使用集合从数组中查找最大值的简单方法:
如果您有兴趣查找最小值,类似于查找最大值:
最简单line 对列表进行排序:
或者使用 Arrays 类对数组进行排序:
但是 Arrays 类没有直接引用最大值的方法,对其进行排序并引用最后一个索引是最大值,但是请记住,通过上述 2 种方法进行排序的复杂度为 O(n log n)。
If you are looking for the quickest and simplest way to perform various actions in regards to arrays, the use of the Collections class is extremely helpful (documentation available from https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html), actions ranges from finding the maximum, minimum, sorting, reverse order, etc.
A simple way to find the maximum value from the array with the use of Collections:
If you are interested in finding the minimum value, similar to finding maximum:
The simplest line to sort the list:
Or alternatively the use of the Arrays class to sort an array:
However the Arrays class does not have a method that refers to the maximum value directly, sorting it and referring to the last index is the maximum value, however keep in mind sorting by the above 2 methods has a complexity of O(n log n).
与其他人的建议相同,只是提到更清洁的方法:
Same as suggested by others, just mentioning the cleaner way of doing it:
获得数组最大值的更短解决方案:
A shorter solution to have the max value of array:
您的 print() 语句位于
for()
循环中,它应该位于之后,以便只打印一次。按照目前的方式,每次最大值更改时都会打印一个max
。You have your print() statement in the
for()
loop, It should be after so that it only prints once. the way it currently is, every time the max changes it prints amax
.如果你不想使用任何java预定义库那么下面是
最简单的方法
If you don't want to use any java predefined libraries then below is the
simplest way
您可以使用接受数组并查找其中最大值的函数。我将其设为通用,因此它也可以接受其他数据类型
You can use a function that accepts a array and finds the max value in it. i made it generic so it could also accept other data types
您只需将第零个元素与其余元素进行比较,以便它将打印它将包含的最后一个最大值,在您的情况下,会发生同样的问题。要比较每个元素,我们必须交换值,例如:
希望这对您有帮助
You are just Compare zeroth element with rest of the elements so it will print the value last largest value which it will contain, in you case, same problem is happening. To compare each and every elements we have to swap the values like:
Hope this will help you
我发现的最简单的方法,支持所有Android版本
Easiest way which I've found, supports all android versions
不使用集合的简单方法
An easy way without using collections
找到最大数量的简单方法
simple way find max number