繁体   English   中英

main类型的方法(double [])不适用于参数(int [])

[英]The method (double[]) in the type main is not applicable for the arguments (int[])

我试图找到Java中数组的中位数。

我得到了例外:

线程“主”中的异常java.lang.Error:未解决的编译问题:

tez3类型的中位数(double [])方法不适用于参数(int [])

在tez3.main(tez3.java:33)

我的代码如下。 问题是什么? 如何打印双重功能?

import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;

public class tez3 {

    public static void main(String args[]) throws java.io.IOException {
        Scanner s = new Scanner(new File("C:\\tny\\Deneme1.txt"));
        int[] numberList = new int[10];
        int i = 0;
        int count = 0;
        int result = 0;
        while (s.hasNextInt()) {
            numberList[i++] = s.nextInt();
        }
        for (i = 0; i < numberList.length; i++) {
            System.out.println(+(i + 1) + ".Value: " + numberList[i]);
        }
        for (i = 0; i < numberList.length; i++) {
            count++;
        }

        for (i = 0; i < numberList.length; i++) {
            result += numberList[i];
        }

        System.out.println("Average of the Values is: " + result / count);
        System.out.println("Mode of the Values is: " + mode(numberList));
        System.out.println("Median of the Values is: " + median(numberList));
    }

    public static int mode(int numberList[]) {
        int maxValue = 0, maxCount = 0;

        for (int i = 0; i < numberList.length; ++i) {
            int count = 0;
            for (int j = 0; j < numberList.length; ++j) {
                if (numberList[j] == numberList[i]) {
                    ++count;
                }
            }
            if (count > maxCount) {
                maxCount = count;
                maxValue = numberList[i];
            }
        }

        return maxValue;
    }

    public double median(double[] numberList) {
        int factor = numberList.length - 1;
        double[] first = new double[(double) factor / 2];
        double[] last = new double[first.length];
        double[] middleNumbers = new double[1];
        for (int i = 0; i < first.length; i++) {
            first[i] = numbersList[i];
        }
        for (int i = numberList.length; i > last.length; i--) {
            last[i] = numbersList[i];
        }
        for (int i = 0; i <= numberList.length; i++) {
            if (numberList[i] != first[i] || numberList[i] != last[i]) {
                middleNumbers[i] = numberList[i];
            }
        }
        if (numberList.length % 2 == 0) {
            double total = middleNumbers[0] + middleNumbers[1];
            return total / 2;
        } else {
            return middleNumbers[0];
        }
    }
}

问题恰恰是错误消息中所说的。 您的方法mean()采用double []作为参数。

但是,当您尝试调用它时,可以给它numberList作为参数。 但是numberList是一个int [],也就是说它不是double []。

最简单的解决方法是修改mid(),使它采用的参数是int []而不是double []。

问题是您要将int []列表中的所有元素都强制转换为双精度。 如果您不想将中位数的参数更改为int [],那么我处理此问题的首选方法是重载方法并转换参数

因此,它将类似于:

    public double median(int[] numberList) {
        double[] doubleList = new double[numberList.size()];
        for(int i=0; i<doubleList.size(); i++){
            doubleList[i] = (double)numberList[i];
        }
        return median(doubleList);
    }

如果确实更改了原始“ median”方法的参数,则每次调用它时都将强制转换numberList元素,即

    first[i] = (double) numbersList[i];

暂无
暂无

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

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