簡體   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