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
  • 相关阅读:
    【bzoj2006】超级钢琴
    【bzoj4940】这是我自己的发明
    【arc076E】Connected?
    【agc004C】AND Grid
    选举
    几何
    打击目标
    【CF Gym100228】Graph of Inversions
    【CodeChef】Chef and Graph Queries
    大包子玩游戏
  • 原文地址:https://www.cnblogs.com/sea-stream/p/12098740.html
Copyright © 2011-2022 走看看