gpt4 book ai didi

java - 如何从数组中选择最大值?

转载 作者:行者123 更新时间:2023-12-02 04:06:23 26 4
gpt4 key购买 nike

我正在创建一个初等数学教程,对于这一部分,我有一个数组,该数组填充了 0 到 9 之间的 5 个随机数。我创建的问题将给出 5 个随机数字,并说“什么”你能用这些数字得出的最大数字是什么?”,然后我将该数字存储在变量中

    // These are all Random numbers using the Random method
a = num.nextInt(9);
b = num.nextInt(9);
c = num.nextInt(9);
d = num.nextInt(9);
e = num.nextInt(9);
f = num.nextInt(9);

// Asks the question right here (not important right now)
// prints out 5 digits here (again, not important right now)

g = ans.nextInt(); // this is the users response

int h3[] = {a, b, c, d, e, f}; // this array holds the random numbers

最佳答案

在java8中

 IntStream.of(h3).max().getAsInt();

关于java - 如何从数组中选择最大值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34241247/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com