简体   繁体   English

有没有办法获得随机整数的 Math.min() ? 爪哇

[英]Is there a way to get the Math.min() for random integers? Java

I am currently making a game and I'm trying to compare the shortest path that is displayed on screen (which is a random generated path of integers) and the path that the user takes.我目前正在制作一个游戏,我正在尝试比较屏幕上显示的最短路径(这是一个随机生成的整数路径)和用户采用的路径。 This part of the code is the one in which I try to get the Math.min() of the total paths on screen:这部分代码是我尝试在屏幕上获取总路径的Math.min()部分:

int path1 = randomNum3 + randomNum8;
int path2 = randomNum2 + randomNum7;
int path3 = randomNum + randomNum6;
int path4 = randomNum3 + randomNum5 + randomNum7;
int path5 = randomNum2 + randomNum5 + randomNum8;
int path6 = randomNum + randomNum4 + randomNum7;
int path7 = randomNum2 + randomNum4 + randomNum6;


int caminoMasCorto = Math.min(path1, path2, path3, path4, path5,
        path6, path7);

The error that is showing me is this one:向我展示的错误是这个:

no suitable method found for min(int,int,int,int,int,int,int)没有找到适合 min(int,int,int,int,int,int,int) 的方法

method Math.min(int,int) is not applicable方法 Math.min(int,int) 不适用

(actual and formal argument lists differ in length) (实际和形式参数列表的长度不同)

Yes.是的。 As the error says, Math.min(int, int) takes two arguments.正如错误所说, Math.min(int, int)需要两个参数。 Change改变

int caminoMasCorto = Math.min(path1, path2, path3, path4, path5,
        path6, path7);

to

int caminoMasCorto = Math.min(path1, Math.min(path2, 
        Math.min(path3, Math.min(path4, Math.min(path5, Math.min(path6, path7))))));

or (as Andreas noted)(正如安德烈亚斯所指出的)

int caminoMasCorto = Math.min(Math.min(Math.min(Math.min(Math.min(Math.min(
        path1, path2), path3), path4), path5), path6), path7);

Alternatively, you would write your own min function that takes an arbitrary number of int (s) and returns the smallest.或者,您可以编写自己的min函数,该函数接受任意数量的int (s) 并返回最小值。 Like,喜欢,

private static int myMin(int... vals) {
    if (vals == null || vals.length < 1) {
        throw new RuntimeException("No values");
    }
    int t = vals[0];
    for (int i = 1; i < vals.length; i++) {
        t = Math.min(t, vals[i]);
    }
    return t;
}

And then use然后使用

int caminoMasCorto = myMin(path1, path2, path3, path4, path5,
        path6, path7);

Use whichever you find most readable.使用您认为最易读的任何一个。

To find the minimum of a lot of int variables, use:要找到大量int变量中的最小值,请使用:

int caminoMasCorto = IntStream.of(path1, path2, path3, path4, path5,
        path6, path7).min().getAsInt();

If you're not using Java 8+, you can do this:如果您不使用 Java 8+,则可以执行以下操作:

int caminoMasCorto = path1;
for (int value : new int[] { path2, path3, path4, path5, path6, path7 })
    caminoMasCorto = Math.min(caminoMasCorto, value);

Java's Math.min accepts two arguments. Java 的Math.min接受两个参数。 To extend this to multiple numbers, try this method:要将其扩展到多个数字,请尝试以下方法:

public int minExtension (int... numbers) {
    int minimum = numbers[0];

    for (int number : numbers) {
        minimum = Math.min(number, minimum)
    }

    return minimum
}

Call it like this像这样称呼

int caminoMasCorto = minExtension (path1, path2, path3, path4, path5, path6, path7);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM