猿问

计算整数序列的最大值和最小值的程序

在我的代码中,我试图从用户输入的数字序列中找到序列的平均值、最大数和最小数。我的问题是我不知道如何编写代码,所以当用户键入 0 或负整数时停止并进行计算。


public class Sequence2

{


    public static void main(String[] args)

    {

        Scanner keyb = new Scanner(System.in);

        int count = 0;

        double sum = 0;

        double avg = 0;

        int n;


        System.out.println("Enter a sequence of positive integers, which will end with either 0 or a negative integer. ");

        int max = keyb.nextInt();

        int min = keyb.nextInt();

        while (keyb.hasNextInt())

        {

            n = keyb.nextInt();

            if (n > max)

            {

                max = n;

            }

            if (n < min)

            {

                min = n;

            }

            count++;

            sum += n;

        }

        System.out.println("The maximum value is: " + max);

        System.out.println("The minimum value is: " + min);

        avg = sum / count;

        System.out.println("Average = " + avg);

    }

}


开满天机
浏览 143回答 4
4回答

暮色呼如

解决这个问题的关键点是维护在循环外定义的最小值和最大值的状态。特别是,最小值应该用最大可能的整数值作为种子,而最大值可以用零作为种子。public static void main(String[] args) {&nbsp; &nbsp; int minimum = Integer.MAX_VALUE;&nbsp; &nbsp; int maximum = 0;&nbsp; &nbsp; int total = 0;&nbsp; &nbsp; int counter = 0;&nbsp; &nbsp; Scanner keyb = new Scanner(System.in);&nbsp; &nbsp; while (true) {&nbsp; &nbsp; &nbsp; &nbsp; int num = keyb.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; if (num <= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Exiting input loop.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (num < minimum) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; minimum = num;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (num > maximum) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maximum = num;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; total += num;&nbsp; &nbsp; &nbsp; &nbsp; ++counter;&nbsp; &nbsp; }System.out.println("minimum: " + minimum);System.out.println("maximum: " + maximum);System.out.println("average: " + (total / counter));我省略了一些事情,例如处理用户没有在扫描仪中输入有效整数的可能性。此外,我假设用户总是会输入至少一个有效整数。如果不是,我们将不得不在最后处理平均算术以避免被零除。

慕勒3428872

做这件事有很多种方法。如果我们想通过对现有代码进行最少的更改来做到这一点,我会说最简单的选择是添加一个布尔标志,指示是否继续循环:在你的 while 循环之前boolean&nbsp;userWantsToQuit&nbsp;=&nbsp;false;这应该是你的循环条件:while&nbsp;(keyb.hasNextInt()&nbsp;&&&nbsp;!userWantsToQuit)然后在您的 while 循环中,您需要在其他 if 语句之前添加此 if 语句if(n<=0){userWantsToQuit=true}您可能还想将其他 if 语句切换为 else if 语句附加到此条件 - 否则您将始终得到 0 或您用来退出的负数作为输入的最小数字。您还可以使用 break 语句而不是布尔标志 - 这是个人喜好问题。出于某种原因,我总是被教导要避免 break 语句,所以我通常使用标志,但两者都是有效的方法。这不是最惯用的解决方案,但它是最相似地遵循您已经设置的模式的解决方案。

饮歌长啸

如果你想让它更像 Java-8,就像一个想法。首先,您从用户那里收集所有整数List<Integer>然后计算IntSummaryStatistics并提取最小值、最大值和平均值:result = list.stream().collect(Collectors.summarizingInt(Integer::intValue));min = result.getMin();max = result.getMax();average = result.getAverage();

慕姐8265434

您走在正确的轨道上,但是您不需要avg在代码顶部预先声明变量,因为它只是在最后计算的。对于循环退出条件,当输入小于或等于零时,使用 if 语句中断 while 循环:import java.util.Scanner;public class Sequence2 {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int count = 0, overallMax = 0, overallMin = Integer.MAX_VALUE;&nbsp; &nbsp; &nbsp; &nbsp; double sum = 0;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter a sequence of positive integers, which will end with either 0 or a negative integer. ");&nbsp; &nbsp; &nbsp; &nbsp; while (scanner.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (scanner.hasNextInt()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int num = scanner.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (num <= 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanner.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; overallMax = Math.max(overallMax, num);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; overallMin = Math.min(overallMin, num);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += num;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("ERROR: Invalid Input. Enter a integer!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanner.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The maximum value is: " + overallMax);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The minimum value is: " + overallMin);&nbsp; &nbsp; &nbsp; &nbsp; double avg = sum / count;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Average: " + avg);&nbsp; &nbsp; }}用法示例:Enter a sequence of positive integers, which will end with either 0 or a negative integer.4aERROR: Invalid Input. Enter a integer!26-1The maximum value is: 6The minimum value is: 2Average: 4.0
随时随地看视频慕课网APP

相关分类

Java
我要回答