【发布时间】:2019-06-01 14:30:52
【问题描述】:
我正在练习使用数组,我现在只想打印这个数组的最大值,但我无法弄清楚,我已经尝试过查看所有内容。请有人解释一下。
import java.util.*;
class Practice
{
public static void main(String[] args)
{
int[] Array = {5, 7, 2, 10};
}
public static int getMaxValue(int[] Array)
{
int maxValue = Array[0];
for (int i = 1; i < Array.length; i++)
{
if (Array[i] > maxValue)
{
maxValue = Array[i];
}
}
return maxValue;
}
}
它编译没有错误,但不打印最大值。
【问题讨论】:
-
为什么你认为它应该打印任何东西?
-
你似乎从来没有给
getMaxValue打电话,或者在任何地方打印出来。 -
一切正常 ;) ...你只是忘了打电话给
getMaxValue。只需将int[] Array = {5, 7, 2, 10};替换为System.out.printf("%s", getMaxValue(new int[] {5, 7, 2, 10})); -
旁注:您找到最大值的代码不正确。数组从 0 开始计数,因此您通过在 1 处开始 for 循环来跳过第一个值
-
不是——第一个值是在循环上方读取的。
标签: java arrays output max min