繁体   English   中英

在不使用Array的情况下查找列表中的两个最大数字

[英]Find Two largest numbers in a list without using Array

我被困在家里。 基本上,我们需要创建一个程序来查找用户输入的列表中的最大和最小整数。当用户输入0时停止。但是我们不允许用户数组来解决这个问题。

还有一个条件:如果最大值出现多次,则该值应列为最大值和第二大值,如下面的示例运行所示。

我遇到了该计划的第一个条件,但我无法满足第二个条件。

import java.util.Scanner;
public class FindTwoLargest {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int num = 1, max1st = 0, max2nd = 0;
        int count = 1;
        System.out.println("This program allows the user to create a list integers until the user enters 0");
        System.out.println("The program will print the Largest value and the Second largest value from the list!");
        while(num != 0) {
            System.out.print("Integer No." + count + " ");
            num = sc.nextInt();

            if(num >= max1st && num >= max2nd) {
                max1st = num;
            }
            if(num >= max2nd && num < max1st) {
                max2nd = num;
            }
            count++;
        }
        System.out.println("The largest value is " + max1st);
        System.out.println("The second largest value is " + max2nd);

   }
}

我试过使用这段代码。

 if(num >= max2nd && num <= max1st) {
max2nd = num;
            }

但是,当我运行程序https://imgur.com/a/YMxj9qm时 - 显示。

它应该打印75和45。 我能做些什么来满足第一个条件并满足第二个条件?

如果超过最大数量( max1st ),则新的最大数量将设置为num 但您的第二大数字将是当前的最大数字。 所以试试这个条件:

if (num > max1st) {
    max2nd = max1st;
    max1st = num;
} else if (num > max2nd) {
    max2nd = num;
}

请用户如果是那样的情况。 您使用了两个替换max2nd值的if语句。

    import java.util.Scanner;
    public class FindTwoLargest {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int num = 1, max1st = 0, max2nd = 0;
        int count = 1;
        System.out.println("This program allows the user to create a list integers until the user enters 0");
        System.out.println("The program will print the Largest value and the Second largest value from the list!");
        while(num != 0) {
            System.out.print("Integer No." + count + " ");
            num = sc.nextInt();

            if(num >= max1st && num >= max2nd) {
                max1st = num;
            }
            else if(num >= max2nd && num < max1st) {
                max2nd = num;
            }
            count++;
        }
        System.out.println("The largest value is " + max1st);
        System.out.println("The second largest value is " + max2nd);

   }
}

我改变了条件,请看看

import java.util.Scanner;
public class FindTwoLargest {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int num = 1, max1st = 0, max2nd = 0;
        int count = 1;
        System.out.println("This program allows the user to create a list integers until the user enters 0");
        System.out.println("The program will print the Largest value and the Second largest value from the list!");
        num = sc.nextInt();
        while(num != 0) {
            System.out.print("Integer No." + count + " ");

            if(num > max1st) {
                if(max1st > max2nd) {
                    max2nd = max1st;
                }
                max1st = num;
            } else {
                if(num > max2nd) {
                    max2nd = num;
                }
            }
            count++;
            num = sc.nextInt();
        }
        System.out.println("");
        System.out.println("The largest value is " + max1st);
        System.out.println("The second largest value is " + max2nd);

   }
}

这是一个更简单的方法。

import java.util.Scanner;
public class FindTwoLargest {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int num = 1, max1st = 0, max2nd = 0;
        do
        {
            num = sc.nextInt();
            max2nd = (num >= max1st) ? max1st : (num > max2nd) ? num : max2nd;
            max1st = num > max1st ? num : max1st;
        }
        while(num != 0)

        System.out.println("\nThe largest value is " + max1st);
        System.out.println("The second largest value is " + max2nd);

   }
}

如果您需要更多解释,我将很乐意为您提供帮助。

您可以尝试以下方法,这应该可以解决您的问题=>

import java.util.*;
public class BackedList {

    private static Scanner sc;

    public static void main(String args[]){
        sc = new Scanner(System.in);
        int num = 1;
        List<Integer> integersList = new ArrayList<Integer>();
        int count = 1;
        System.out.println("This program allows the user to create a list integers until the user enters 0");
        System.out.println("The program will print the Largest value and the Second largest value from the list!");
        while(num != 0) {
            System.out.print("Integer No." + count + " ");
            num = sc.nextInt();

            integersList.add(num);
        }

        Collections.sort(integersList, Collections.reverseOrder());
        if(integersList.get(0) == integersList.get(1)){
            System.out.println("The number " + integersList.get(0) + " is first largest and " 
        + integersList.get(1) + " is the second largest number");
        }
        else 
        {
            System.out.println("The largest number is :" + integersList.get(0) + " and the smallest one is :"
        + integersList.get(integersList.size()-1));
        }
    }

}

您可以使用该集合来获得最大和最大的像:

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(5);
list.add(2);
Collections.sort(list);
int firstLargest = list.get(list.size()-1);
int secLargest = list.get(list.size()-2);
import java.util.Scanner;
public class FindTwoLargest {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int num = 1, max1st = 0, max2nd = 0;
        int count = 1;
        System.out.println("This program allows the user to create a list integers until the user enters 0");
        System.out.println("The program will print the Largest value and the Second largest value from the list!");
        while(num != 0) {
            System.out.print("Integer No." + count + " ");
            num = sc.nextInt();

            if(num > max1st) {
                max2nd=max1st;
                max1st = num;
            }
           else if(num == max1st ) {
                max2nd = num;
            }
            count++;
        }
        System.out.println("The largest value is " + max1st);
        System.out.println("The second largest value is " + max2nd);

   }
}

在你的if中,用&&过度复杂化了。 如果一个数字大于第一个,那么如果一个数字与第一个第二个相同,那么如果它没有更大或相同的任何事情都没有发生变化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM