当前位置: 首页 > 知识库问答 >
问题:

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

冯宪
2023-03-14

我的代码要求用户输入动物物种的值,然后将其显示给他们。我只需要最后添加一个部分,它也会告诉用户最濒危的动物(输入的数字最低)。我已经环顾了一些地方,并使用x进行了triec

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]);
                }
            }
        }
    }
}    

共有3个答案

端木元青
2023-03-14

你没有在任何地方计算最大值。

替换:

int max = array[j]

具有:

max = max > array[j] ? max : array[j];

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

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

for(int i = 0; i <= 4; i++) { 
   if(array[i] == max) {
       System.out.println("Max endangered:" + array[i] + name[i]);
   }
}
微生俊材
2023-03-14

在java 8中,你可以这样做:

int min = Arrays.stream(array).reduce(Integer.MAX_VALUE, Math::min);

更新:用户也要求打印动物。

如果您还需要返回动物,最好我们编辑您的代码。我们将再添加 2 个变量。第一个,minVal 将包含用户输入的最低数字。第二个,最小值索引将包含计数最低的物种的索引。我删除了你的同时周期,因为不需要一个。

  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]);
    }
田鸿彩
2023-03-14

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

Arrays.sort(array);
int max = array[array.length - 1];
int min = array[0];

有关Java的<code>数组

公共静态空隙排序(int[] a)

将指定数组按数字升序排序。

参数:a - 要排序的数组

如果你想得到相应的动物,那么我建议忽略上述内容,并在Java 8中使用流。将字符串和 int 数组组合到一个 2D 字符串数组中。使行等于动物的数量,使列等于 2(例如:对于 5 只动物,字符串[][] 数组 = 新的字符串[5][2])。对于 2D 数组中的每一行,第一个元素应该是动物,第二个元素应该是大小(例如:字符串 [][] 数组 = {{“狐狸”、“1”}、{“鹿”、“0”}、{“熊”、“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
 类似资料:
  • 我有一个家庭作业,要求我使用输入对话框要求最终用户输入一个数字,将该字符串解析为int,然后使用确认对话框询问用户是否要输入另一个数字。如果单击“是”,程序将循环,一旦单击“否”,它将获取所有输入的数字,并确定最小值和最大值。如何跟踪所有输入的数字,而不声明多个变量来保存它们(因为从技术上讲,用户可以输入无限数量的数字)? 我对编程很陌生。我一直在搜索我的教科书,我浏览了堆栈溢出来找到我的具体问题

  • 我有一些代码,其中多个方法使用键盘,并在主方法中连续调用。我正在做的练习特别要求使用4种不同的方法,所以我不能把它们放在一起。最初,我用键盘。在每个方法的末尾关闭(),但当第二个方法运行时,无论调用顺序如何,这都会导致NoTouchElementException。通过卸下键盘。close(),代码现在可以工作了,但是我现在收到了资源泄漏的警告,因为键盘没有关闭。有人能告诉我一种关闭输入而不出错的

  • 问题内容: 我被分配编写一个程序,该程序读取一系列整数输入并打印-输入的最小和最大-以及偶数和奇数输入的数量 我想出了第一部分,但对如何使程序显示最大和最小感到困惑。到目前为止,这是我的代码。我怎样才能显示最小的输入呢? 问题答案: 最简单的解决方案是使用诸如和

  • 问题内容: 我正在上课的作业,正在寻找一些有用的指示,而不是完整的解决方案。基本上,我必须编写一个Java程序,该程序读取一个文本文件并逐行列出信息,列出行号,最后打印出最大值和最小值以及与每个值相关的年份。文本文件包含一年和该年的温度。因此,它列出了类似“ 1900 50.9”的内容。我不是要使用数组或扫描仪,这是分配的一部分。我已经能够成功地使程序逐行打印并逐行打印出相应的温度。有人告诉我,我

  • 你好,我是一个初学写c的人,我想知道你将如何写一个代码,提示你给一个正整数,并在你输入0后结束,0之前的数字至少是2个正整数。然后,程序确定最大正整数输入和最小整数输入。 起初,我试图使用do/while循环,但它根本不起作用。 我还对当循环中有if ese语句时编译器如何读取循环感到困惑。 我是这样想的:我想让“输入正整数”重复询问,直到你输入两个正数。然后我会让程序检查下一个输入是否等于零,如