0

首先,我很新。所以,当一切都是 int 时,我能够让它运行,但是由于某种原因将它更改为 double,我只能输入第一个值并且它出错。我不明白为什么我不能这样做:

        int datVal = Integer.parseInt(inNum);

        double [] iOne;
        iOne = new double[datVal];

在我运行它或调试它之前没有错误。这是我所拥有的:

import java.util.Arrays;

import javax.swing.JOptionPane;

public class arStats2 {
    public static void main(String[] args) {
        double med, men, min, max;
        String inNum = JOptionPane.showInputDialog("Enter the number of data values:");

        int datVal = Integer.parseInt(inNum);

        double [] iOne;
        iOne = new double[datVal];

        double [] iTwo;
        iTwo = new double[datVal];

        int index=0;

        for (index=0; index < iOne.length; index++) {

            String value = JOptionPane.showInputDialog("Input a data value:");
            int valIn = Integer.parseInt(value);
            iOne[index] = valIn;
        }

        System.arraycopy(iOne, 0, iTwo, 0, iOne.length);
        Arrays.sort(iOne);

        String out1 = "Sorted data: ";
        for (int i=0; i<iOne.length; i++) {
            out1 = out1 +iOne[i]+"";
        }
        out1 = out1 +"\n";

        String out2 = "Original data: ";
        for (int i2=0; i2<iTwo.length; i2++) {
            out2 = out2 +iTwo[i2] + "";
        }
        out2 = out2 +"\n";

        med = median(iOne);
        men = mean(iTwo);
        max = computeMax(iOne);
        min = computeMin(iOne);

        JOptionPane.showMessageDialog(null, out2 +out1 +"Min Value: " +min +"\n"
                                        +"Max value: " +max +"\n"
                                        +"Median value: " +med +"\n"
                                        +"Mean value: " +men +"\n");
        }

    public static double median(double[] iOne) {

        double med;
        int index, indexHi, indexLo;
        if ((iOne.length %2) !=0) {
            index = iOne.length / 2;
            med = iOne[index];
        }
        else {
            indexHi = iOne.length / 2;
            indexLo = indexHi = 1;
            med = (iOne[indexLo] + iOne[indexHi])/2;
        }
        return med;
    }

    public static double mean(double[] iOne) {
        double sum = 0;
        for (int i = 0; i < iOne.length; i++) {
            sum += iOne[i];
        }
        return sum / iOne.length;
    }

    public static double computeMax(double[] iOne) {
        double max = (iOne.length - 1);
        return max;
    }

    public static double computeMin(double[] iOne) {
        double min;
        min = iOne[0];
        return min;
    }
    }

错误是:

Exception in thread "main" java.lang.NumberFormatException: For input string: "7.2"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:456)
    at java.lang.Integer.parseInt(Integer.java:497)
    at arStats2.main(arStats2.java:25)
4

3 回答 3

2

您需要在此处将值解析为 double :

 for (index=0; index < iOne.length; index++) {

        String value = JOptionPane.showInputDialog("Input a data value:");
        double valIn = Double.parseDouble(value); // this line is changed
        iOne[index] = valIn;
    }

正如 ValarDohaeris 所指出的,这将使其适用于双值,但您的代码中还存在其他问题。

于 2013-04-29T20:05:28.500 回答
0

如果您可以更具体地了解该错误,将会很有帮助。但是,我认为代码中有两部分是错误的:

indexLo = indexHi = 1;

应该

indexLo = indexHi - 1;

computeMax()应该有:

double max = iOne[iOne.length - 1];
于 2013-04-29T20:05:40.687 回答
-1

但由于某种原因将其更改为双倍,我只能输入第一个值并且它会出错。

好吧,原因是datVal = Integer.parseInt(inNum);需要一个 Integer 并且您正在向它传递一个包含 double 的字符串,这将导致 抛出NumberFormatException 。

你应该用一个try{} catch {}块包围它并为此做点什么。

于 2013-04-29T20:09:48.907 回答