Java 如何确定已输入的最小值?

Java 如何确定已输入的最小值?,java,Java,我的代码要求用户输入动物种类的值,然后显示给他们。我只需要最后添加一个部分,该部分还告诉用户最濒危的动物(该动物将是输入的最低数字)。我环顾了一些地方,并使用x

我的代码要求用户输入动物种类的值,然后显示给他们。我只需要最后添加一个部分,该部分还告诉用户最濒危的动物(该动物将是输入的最低数字)。我环顾了一些地方,并使用x
import java.util.Scanner;

public class endangered
{
    public static void main(String[] param)
    {
        animal();
        System.exit(0);
    }

    public static void animal()
    {
        int[] array = new int[5];
        int j = 0;
        String[] names = {"Komodo Dragon" , "Manatee" , "Kakapo" , "Florida Panther" , "White Rhino"};
        System.out.println("Please enter the number of wild Komodo Dragons, Manatee, Kakapo, Florida Panthers and White Rhinos.");
        Scanner scan = new Scanner(System.in);
        for (int i=0; i<=4; i++)
        {
            while(scan.hasNextInt())
            {
                array[j] = scan.nextInt();
                int max = array[j];
                j++;
                if(j==5)
                {
                    System.out.println(array[0] + ", " + names[0]);
                    System.out.println(array[1] + ", " + names[1]);
                    System.out.println(array[2] + ", " + names[2]);
                    System.out.println(array[3] + ", " + names[3]);
                    System.out.println(array[4] + ", " + names[4]);
                }
            }
        }
    }
}    
import java.util.Scanner;
公共阶层濒临灭绝
{
公共静态void main(字符串[]参数)
{
动物();
系统出口(0);
}
公共动物
{
int[]数组=新的int[5];
int j=0;
字符串[]名称={“科莫多龙”、“海牛”、“卡卡波”、“佛罗里达豹”、“白犀牛”};
println(“请输入野生科莫多龙、海牛、卡卡波、佛罗里达豹和白犀牛的数量。”);
扫描仪扫描=新扫描仪(System.in);

对于(int i=0;i您可以使用内置方法对数组进行排序,然后检索最后一个值(最大值)和第一个值(最小值)

有关Java的
Arrays
类中的
sort
方法的更多信息,下面介绍它的具体功能

公共静态无效排序(int[]a)

将指定数组按升序进行排序

参数: a-要排序的数组

如果您想要获得相应的动物,那么我建议忽略上述内容,并在Java 8中使用streams。将字符串和int数组组合成一个2D字符串数组。使行等于动物数量,列等于2(例如:对于5个动物,
String[][]数组=新字符串[5][2]
)。对于2D数组中的每一行,第一个元素应该是动物,第二个元素应该是大小(例如:
String[]array={{“fox”,“1”},{“deer”,“0”},{“bear”,“2”}
)。下面将返回一个按升序排序的2D数组,并为您提供相应的动物

String[][] sorted = Arrays.stream(array)
.sorted(Comparator.comparing(x -> Integer.parseInt(x[1])))
.toArray(String[][]::new);
String smallestAnimal = sorted[0][0]; //name of smallest animal
String smallest = sorted[0][1]; //population of smallest animal
String biggestAnimal = sorted[sorted.length - 1][0]; //name of biggest animal
String biggest = sorted[sorted.length - 1][1]; //population of biggest animal

您可以使用内置方法对数组进行排序,然后检索最后一个值(最大值)和第一个值(最小值)

有关Java的
Arrays
类中的
sort
方法的更多信息,下面介绍它的具体功能

公共静态无效排序(int[]a)

将指定数组按升序进行排序

参数: a-要排序的数组

如果您想要获得相应的动物,那么我建议忽略上述内容,并在Java 8中使用streams。将字符串和int数组组合成一个2D字符串数组。使行等于动物数量,列等于2(例如:对于5个动物,
String[][]数组=新字符串[5][2]
)。对于2D数组中的每一行,第一个元素应该是动物,第二个元素应该是大小(例如:
String[]array={{“fox”,“1”},{“deer”,“0”},{“bear”,“2”}
)。下面将返回一个按升序排序的2D数组,并为您提供相应的动物

String[][] sorted = Arrays.stream(array)
.sorted(Comparator.comparing(x -> Integer.parseInt(x[1])))
.toArray(String[][]::new);
String smallestAnimal = sorted[0][0]; //name of smallest animal
String smallest = sorted[0][1]; //population of smallest animal
String biggestAnimal = sorted[sorted.length - 1][0]; //name of biggest animal
String biggest = sorted[sorted.length - 1][1]; //population of biggest animal

在java 8中,您可以执行以下操作:

int min = Arrays.stream(array).reduce(Integer.MAX_VALUE, Math::min);
更新:用户还要求打印动物

如果您也需要返回动物,最好我们编辑您的代码。 我们将再添加两个变量。第一个变量minVal将包含用户输入的最低数量。第二个变量minValIndex将包含数量最低的物种索引。 我删除了你的while循环,因为不需要

  public static void animal()
    {
        int[] array = new int[5];
        int j = 0;
        String[] names = {"Komodo Dragon" , "Manatee" , "Kakapo" , "Florida Panther" , "White Rhino"};
        System.out.println("Please enter the number of wild Komodo Dragons, Manatee, Kakapo, Florida Panthers and White Rhinos.");
        Scanner scan = new Scanner(System.in);
        int minVal = Integer.MAX_VALUE;
        int minValIndex = -1;
        for (int i=0; i<=4; i++)
        {
                array[j] = scan.nextInt();
                if(array[j] < minVal) {
                    minVal = array[j];
                    minValIndex = j;
                }
                int max = array[j];
                j++;
                if(j==5)
                {
                    System.out.println(array[0] + ", " + names[0]);
                    System.out.println(array[1] + ", " + names[1]);
                    System.out.println(array[2] + ", " + names[2]);
                    System.out.println(array[3] + ", " + names[3]);
                    System.out.println(array[4] + ", " + names[4]);
                }

        }
        System.out.println("The smallest entered number:" + minVal);
        System.out.println("The species:" + names[minValIndex]);
    }
publicstaticvoidanimal()
{
int[]数组=新的int[5];
int j=0;
字符串[]名称={“科莫多龙”、“海牛”、“卡卡波”、“佛罗里达豹”、“白犀牛”};
println(“请输入野生科莫多龙、海牛、卡卡波、佛罗里达豹和白犀牛的数量。”);
扫描仪扫描=新扫描仪(System.in);
int minVal=Integer.MAX_值;
int minValIndex=-1;

对于java 8中的(inti=0;i,可以执行以下操作:

int min = Arrays.stream(array).reduce(Integer.MAX_VALUE, Math::min);
更新:用户还要求打印动物

如果您也需要返回动物,最好我们编辑您的代码。 我们将再添加两个变量。第一个变量minVal将包含用户输入的最低数量。第二个变量minValIndex将包含数量最低的物种索引。 我删除了你的while循环,因为不需要

  public static void animal()
    {
        int[] array = new int[5];
        int j = 0;
        String[] names = {"Komodo Dragon" , "Manatee" , "Kakapo" , "Florida Panther" , "White Rhino"};
        System.out.println("Please enter the number of wild Komodo Dragons, Manatee, Kakapo, Florida Panthers and White Rhinos.");
        Scanner scan = new Scanner(System.in);
        int minVal = Integer.MAX_VALUE;
        int minValIndex = -1;
        for (int i=0; i<=4; i++)
        {
                array[j] = scan.nextInt();
                if(array[j] < minVal) {
                    minVal = array[j];
                    minValIndex = j;
                }
                int max = array[j];
                j++;
                if(j==5)
                {
                    System.out.println(array[0] + ", " + names[0]);
                    System.out.println(array[1] + ", " + names[1]);
                    System.out.println(array[2] + ", " + names[2]);
                    System.out.println(array[3] + ", " + names[3]);
                    System.out.println(array[4] + ", " + names[4]);
                }

        }
        System.out.println("The smallest entered number:" + minVal);
        System.out.println("The species:" + names[minValIndex]);
    }
publicstaticvoidanimal()
{
int[]数组=新的int[5];
int j=0;
字符串[]名称={“科莫多龙”、“海牛”、“卡卡波”、“佛罗里达豹”、“白犀牛”};
println(“请输入野生科莫多龙、海牛、卡卡波、佛罗里达豹和白犀牛的数量。”);
扫描仪扫描=新扫描仪(System.in);
int minVal=Integer.MAX_值;
int minValIndex=-1;

对于(int i=0;i而言,您没有在任何地方计算max

替换:

int max = array[j]
与:

存储数组中的最大值。(这是一个三元运算符)

现在数组具有相等的值,因此在这种情况下,请通过以下代码注意这种可能性:

for(int i = 0; i <= 4; i++) { 
   if(array[i] == max) {
       System.out.println("Max endangered:" + array[i] + name[i]);
   }
}

for(int i=0;i您没有在任何地方计算max

替换:

int max = array[j]
与:

存储数组中的最大值。(这是一个三元运算符)

现在数组具有相等的值,因此在这种情况下,请通过以下代码注意这种可能性:

for(int i = 0; i <= 4; i++) { 
   if(array[i] == max) {
       System.out.println("Max endangered:" + array[i] + name[i]);
   }
}

用于(int i=0;我太棒了!谢谢你。@ZainMohamed没问题,如果你觉得答案有用,别忘了投票或接受。快乐编码!可以。还有一件事。你的方法会返回最大的数字还是动物?@ZainMohamed检查我的编辑,确保你使用的是java8@ZainMohamed检查我的新编辑,清除错误!