zoukankan      html  css  js  c++  java
  • java 获取输入的最大的以及第二大的数字

    //find nd greatest among n numbers
    
    import java.util.Scanner;
    
    public class Numbers {
    
        public static void main(String[] args) {
            int n, temp, max = Integer.MIN_VALUE, secondMax = Integer.MIN_VALUE;
            Scanner ip = new Scanner(System.in);
            System.out.print("Enter the number of inputs: ");
            n = ip.nextInt();
            for (int i = 1; i <= n; i++) {
                System.out.print("Enter number " + i + ": ");
                temp = ip.nextInt();
                if (temp >= max) {
                    secondMax = max;
                    max = temp;
                } else if (temp >= secondMax)
                    secondMax = temp;
            }
            System.out.println("The greatest number is " + max);
            System.out.println("The second greatest number is " + secondMax);
            ip.close();
        }
    
    }
    
    
    
    OUTPUT:
    Enter the number of inputs: 7
    Enter number 1: 11
    Enter number 2: 67
    Enter number 3: 23
    Enter number 4: 98
    Enter number 5: 32
    Enter number 6: 45
    Enter number 7: 76
    The greatest number is 98
    The second greatest number is 76
  • 相关阅读:
    Go标准库之tar
    redis必知必会
    GORM CRUD指南
    GORM入门指南
    MUI中tap点击事件点击一次连续申请两次
    Go代码启动默认浏览器
    Go实现JWT
    Go Micro
    protobuf初识
    英语作文
  • 原文地址:https://www.cnblogs.com/sea-stream/p/12098740.html
Copyright © 2011-2022 走看看