【问题标题】:Finding the smallest AND largest number in a list of numbers inputted by the user在用户输入的数字列表中查找最小和最大的数字
【发布时间】:2018-04-26 23:34:17
【问题描述】:

我需要帮助显示用户输入的平均数、最小数和最高数,但我只能显示平均数和最大数。这里还有另一个这样的问题,但没有完全解决,只给出最小的数字。如果有除 Math.min 和 Math.max 之外的其他方式,将不胜感激。

import java.text.DecimalFormat;
import java.util.Scanner;

public class ProblemSet3_1 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    DecimalFormat dec = new DecimalFormat();

    int snum = 0;
    int height = 0;
    int add = 0;
    double avg = 0;
    int largest = 0;
    int smallest = 0; 


    System.out.print("How much students are in your class?" + '\n');
    snum = input.nextInt(); 


    for (int i = 1; i <= snum; i++) {
        System.out.print("How tall is student #" + i + "?" + '\n');
        height = input.nextInt();

        add += height;
        avg = add / i;

        if (height > largest) {
            largest = height;
        }

    }
    System.out.print("The average height is " + avg + ", while the tallest is " + largest + " , and the shortest is " + smallest +  ".");

    }
}

【问题讨论】:

  • 你用谷歌搜索过这个吗?
  • @GBlodgett 是的,我什么都试过了,所以我决定自己问这个问题
  • 你已经得到了最大的,你如何以同样的方式得到最小的?
  • 您期望的数字是否小于 0?

标签: java


【解决方案1】:

是的,有一个使用流的非常简单的方法:

IntSummaryStatistics stats = IntStream.generate(input::nextInt).limit(snum)
    .summaryStatistics();

stats 对象现在保存平均值、计数、最大值、最小值、总和。

编辑:抱歉刚刚意识到您也需要提示消息:

IntStream.rangeClosed(1, snum).map(i -> {
    System.out.println("How tall is student " + i + "?");
    return input.nextInt();}).summaryStatistics();

【讨论】:

  • 正确的答案,但可能是不同的课程:-)
  • @KevinAnderson OP 确实问过是否有不同的方法!
【解决方案2】:

在for循环中,做:

if(i==1){
smallest= height;
largest=height;
}
else {
 if(height< smallest)
   smallest = height
 if(height>largest)
     largest = height
}

【讨论】:

    【解决方案3】:

    可以只添加一个简单的 if/else if 语句

    import java.text.DecimalFormat;
    import java.util.Scanner;
    
    public class ProblemSet3_1 {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            DecimalFormat dec = new DecimalFormat();
    
            int snum = 0;
            int height = 0;
            int add = 0;
            double avg = 0;
            int largest = 0;
            int smallest = 0; 
    
    
            System.out.print("How much students are in your class?" + '\n');
            snum = input.nextInt(); 
    
    
            for (int i = 1; i <= snum; i++) {
                System.out.print("How tall is student #" + i + "?" + '\n');
                height = input.nextInt();
    
                add += height;
                avg = add / i;
                if(i == 1) {
                    smallest = height;
                }
                else if(height < smallest){
                    smallest = height;
                }
                if (height > largest) {
                    largest = height;
                }
    
            }
            System.out.print("The average height is " + avg + ", while the tallest is " + largest + " , and the shortest is " + smallest +  ".");
    
        }
    }
    

    【讨论】:

    • 如果你做if,else if,else if,最初它会设置最大高度当且仅当i首先大于1,所以如果你只输入1个年龄,最大高度将为0,而最小高度可能大于最大高度,因为它被设置为高度。解决这个问题的方法可能是我下面的答案,当 i 等于 1 时,最大设置为高度,最小设置为高度。
    • 如果要跳过if (i == 1)语句,可以将smallest设置为Integer.MAX_VALUE
    猜你喜欢
    • 2014-08-03
    • 2016-03-08
    • 2012-01-26
    • 1970-01-01
    • 2023-03-14
    • 2016-06-13
    • 2016-07-24
    • 1970-01-01
    • 2016-05-25
    相关资源
    最近更新 更多