有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java计算字符串中最高数字的数量

我试图输出用户输入中出现最高数字的次数,例如用户输入2 4 3 4 2 4 0最高数字是4,它出现了3次,不确定如何进行

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner keyboard = new Scanner(System.in);

    String number, last;

    System.out.println("Enter an interger (0 ends the input): ");
    number = keyboard.nextLine();
    last = number.substring(number.length() - 1);

    while(!last.equals("0")){
        System.out.println("Must end the input with a 0: ");
        number = keyboard.nextLine();
        last = number.substring(number.length() - 1);
    }

    String[] array = number.split(" ");

    int max = Integer.MIN_VALUE, maxIndex = 0;

    int count;

    for (int i = 0; i < array.length; i++) {
         if (Integer.parseInt(array[i]) > max) {
             max = Integer.parseInt(array[i]);
             maxIndex = i;
         }
    }
    //String repeat = number.);
    System.out.println("The largest number is " + max);
}

共 (1) 个答案

  1. # 1 楼答案

    其他答案效率较低,因此我将展示suggested solution by David Wallace的代码:

    Well, the basic logic is simple. For each number in the string, there are three possibilities.

    1. It's greater than the previously largest number - so store it as the largest, and reset the count to 1.
    2. It's equal to the previously largest number - so increment the count.
    3. It's less than the previously largest number - so ignore it.

    All you need to do now is convert that to Java.

    这就是:

    int[] input = { 2, 4, 3, 4, 2, 4 };
    
    int count = 0, max = 0;
    for (int value : input) {
        if (count == 0 || value > max) {
            max = value;
            count = 1;
        } else if (value == max) {
            count++;
        }
    }
    
    System.out.printf("Highest number of %d was found %d times%n", max, count);
    

    输出

    Highest number of 4 was found 3 times
    

    如果在很多地方都想这样做,那么可以编写自己的收集器并使用Java8流

    下面是收集器的用法:

    int[] input = { 2, 4, 3, 4, 2, 4 };
    
    Max max = Arrays.stream(input).collect(Max::new, Max::add, Max::merge);
    
    System.out.printf("Highest number of %d was found %d times%n", max.getValue(), max.getCount());
    

    这是收藏家自己:

    public class Max {
        private int value;
        private int count;
        public void add(int newValue) {
            if (this.count == 0 || newValue > this.value) {
                this.value = newValue;
                this.count = 1;
            } else if (newValue == this.value) {
                this.count++;
            }
        }
        public void merge(Max other) {
            if (this.count == 0 || (other.count != 0 && other.value > this.value)) {
                this.value = other.value;
                this.count = other.count;
            } else if (other.value == this.value) {
                this.count += other.count;
            }
        }
        public int getValue() {
            return this.value;
        }
        public int getCount() {
            return this.count;
        }
    }