【问题标题】:Finding the mode of an array: no output查找数组的模式:无输出
【发布时间】:2014-01-10 01:49:50
【问题描述】:

我正在编写一个程序,该程序将 10 个数字作为输入,并使用并行数组显示数字的模式,以及一个将数字数组作为参数并返回数组中最常出现的值的方法。问题:但是,当我运行我的程序时,它绝对没有输出,而且我不确定如何实现并行数组。 有人知道吗? 谢谢你。

import java.util.Scanner;

公开课模式{

public static void main(String[] args) {
}

public int computeMode(int[] nums) {
    Scanner scanner = new Scanner(System.in);

    int maxValue = -1;
    int maxCount = 0;
    int x = 0;
    //count how many times nums[i] appears in array

    System.out.println("Enter 10 numbers: ");
    for (int i = 0; i < 10; i++) {

        try { //try catch exception to catch decimal inputs as well as more /less than 10 integers
            x = scanner.nextInt();
        } catch (Exception e) {
            System.out.println("Invalid input! Please reenter 10 integer values.");
            scanner = new Scanner(System.in);
            i = -1;

            continue;
        }
        for (i = 0; i < nums.length; i++) {
            int count = 0;
            for (int j = 0; j < nums.length; j++) {
                if (nums[j] == nums[i]) {
                    count++;
                }
            }

            if (count > maxCount) {
                maxValue = nums[i];
                maxCount = count;
                System.out.println("The mode is: " + maxValue);
            }

        }

    }
    return maxValue;
}

}

【问题讨论】:

    标签: java arrays try-catch output mode


    【解决方案1】:

    主函数是空的,所以它什么也不做, 并且该函数不需要任何参数,因为您使用扫描仪读取数字

    我认为你需要这个:

    import java.util.Scanner;
    
    class Mode {
        public static void main(String[] args) {
            computeMode();
        }
    
        public static void computeMode(){
            int nums[]=new int[10];
            Scanner scanner = new Scanner(System.in);
    
            int maxValue = -1;
            int maxCount = 0;
            int x = 0;
            //count how many times nums[i] appears in array
    
            System.out.println("Enter 10 numbers: ");
            for (int i = 0; i < 10; i++) {
    
                try { //try catch exception to catch decimal inputs as well as more /less than 10 integers
                    x = scanner.nextInt();
                    nums[i]=x;
                } 
                catch (Exception e) {
                    System.out.println("Invalid input! Please reenter 10 integer values.");
                    i =i -1;
                    scanner.nextLine();
    
                    continue;
                }
            }
            for (int i = 0; i < nums.length; i++) {
                int count = 0;
                for (int j = 0; j < nums.length; j++) {
                    if (nums[j] == nums[i]) {
                        count++;
                    }
                }
    
                if (count > maxCount) {
                    maxValue = nums[i];
                    maxCount = count;
    
                }
    
            }
        System.out.println("The mode is: " + maxValue);
        }
    
    
    }
    

    【讨论】:

    • 啊哈这行得通,谢谢。您是否碰巧知道此方法是否使用并行数组?以及我将如何编辑异常以仅捕获字母或符号(任何不是数字的东西,因为程序只是指定数字......我假设这意味着整数或小数)。 @TurtleLoop
    • 只有一个数组(nums[]),那么这个方法不使用并行数组。这里有一个并行数组的例子:mathbits.com/MathBits/Java/arrays/ParallelArrays.htm
    【解决方案2】:

    代码写入x,但从不读取;读取nums,但从不写入;并实现computeMode,但从不调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 2017-07-12
      相关资源
      最近更新 更多