有 Java 编程相关的问题?

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

java中值和模式显示1

嘿,伙计们,我最近一直在学习编码,我得到了一个任务,在一个整数数组中求多个整数的平均值、中值和模式。我面临的问题是,我的中值和模式显示为-1,我不太确定如何修复它,我的“如果用户输入否,请打印错误声明”也不起作用,如果有人能帮助我,我将非常感激。 这是我的代码:

package com.company;

import java.io.File;
import java.io.FileNotFoundException;

import static com.company.ProjectConstants.*;

import java.util.*;

public class Main {


    public static void main(String[] args) {

        int[] a = new int[MAXDATA];
        int counter = 0;
        boolean fileDone = false;
        boolean inputOk;
        String userInput;
        String theDataFile;

        Scanner s = new Scanner(System.in);

        genProgInfo();

        userInput = s.nextLine();
        userInput = userInput.toLowerCase();

        while (!userInput.equals("yes") && (!userInput.equals("no"))) {
            System.out.println("ERROR: Please input either yes or no: ");
            userInput = s.nextLine();
            userInput = userInput.toLowerCase();
        }

        inputOk = userInput.equals("yes");

        initDataStorage(a);


        //do {
        try {
            // create file & scanner objects
            System.out.println("enter one of the file names:\nData10File.txt\nData30file.txt\nData35file.txt");
            theDataFile = s.next();
            theDataFile = theDataFile.toLowerCase();
            //fName = userInput;
            File f = new File(theDataFile);
            Scanner sc = new Scanner(f);

            // store file data in array, a
            for (int i = 0; i < MAXDATA; i++) {

                if (sc.hasNext()) {
                    a[i] = sc.nextInt();

                } else {

                    fileDone = true;
                    sc.close();
                    break;
                }

            }

            // print error message if file data exceeds the range of array
            if (!fileDone) {
                System.out.println("\n\tCAUTION: file has additional data, consider making array larger.");
            }


        } catch (FileNotFoundException e) {
            System.out.println(e);
            e.printStackTrace();
        }
        //} while (inputOk);


        s.close();

        for (int i=0; i<MAXDATA; i++) {
            if (a[i] != -1) {
                counter = a[i];
            }
        }

        System.out.println("counter: "+ counter);

        displayResults(calcMean(a), calcMedian(a), calcMode(a));


    }


    public static void initDataStorage(int[] data) {

        for (int i = 0; i < MAXDATA; i++) {
            data[i] = INVALID;
        }

    }


    public static double calcMean(int[] data) {


        int counter = 0;
        int mean;
        int sum = 0;

        for (int i = 0; i < MAXDATA; i++) {
            if (data[i] != -1) {
                sum += data[i];
                counter++;
            }
        }


        mean = sum / counter;
        return mean;
    }


    public static double calcMedian(int[] data) {

        int middle = data.length / 2;
        if (data.length % 2 == 1) {
            return data[middle];
        } else {
            return (data[middle -1] + data[middle]) / 2.0;
        }
    }

    public static int calcMode(int[] data) {
        int mode = 0, maxCount = 0;

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

            if (count > maxCount) {
                maxCount = count;
                mode = data[i];

            }
        }


        return mode;
    }


    public static void genProgInfo() {

        System.out.println("This program will calculate the mean, median, and mode of a number of integers stored in the array");
        System.out.println("Would you like to continue?");

    }


    public static void displayResults(double mean, double median, int mode) {

        System.out.println("mean: " + mean);
        System.out.println("median: " + median);
        System.out.println("mode: " + mode);
    }


}

共 (1) 个答案

  1. # 1 楼答案

    根据我看到的其他代码和您对其输出的描述,我假设INVALID的值为-1。数组的长度为MAXDATA,并且最初填充的值在所有元素中都无效。然后用n值填充,其中n可能小于MAXDATA,在这种情况下(可能很常见),数组中的许多甚至大多数值都是无效值

    calcMean函数正确跳过了-1(无效?)值,但不包括在计算中。但是请注意,有效值都在数组的开头,一旦找到无效值,就可以在calcMean中跳出循环

    但是calcMedian和calcMode函数没有考虑无效值。如果n明显小于MAXDATA,那么-1可能真的是模式。你的Calc中值函数还有一个额外的问题,因为需要(有效)数据被排序,以便“中间”或中值在数组的中间。p>

    作业的附加问题:如果输入文件中出现-1怎么办