-3

程序应该从 numberList 中获取数字并计算平均值和频率。我在下面写了我的代码。我有我也发布的主要课程。我知道我可以使用数组,但我想尝试使用数字列表。

import java.io.*;
import java.lang.Math;
import java.util.*;
import java.text.DecimalFormat;


public class intNode {

double average;
double deviation;
int sum = 0;
int number = 0;
int newnumber = 0;
int index;
double frq;

//ArrayList<Integer> element = new ArrayList<Integer>();
int[] rangeFrequency = new int[10];
NumberList numList = new NumberList(51,52,55,57,58,61,62,63,66,66,66,70,72,73,74,75,75,77,77,78,79,81,82,84,86,87,88,91,94,97);
DecimalFormat fmt = new DecimalFormat("0.000");

// Start methods

    public String intAdd (){
        while (numList.hasNextInt())
            {
            element.add(numList.nextInt());
            }
    }

    public int sum (){
        for (int item : element){
            sum += item;
            System.out.println(item);
        }
        return sum;
    }

    public double avg (){
        average = sum / element.size();
        return average;
    }

    public double deviation(){
        for (int i = 0; i < element.size(); i++)
            { 
            newnumber += Math.pow((element.get(i) - average),2);
            }
        deviation = Math.sqrt(newnumber / (element.size()));

        return deviation;
    }

    public String frequency(){
    //int[] rangeFrequency = new int[10];

        for (int elem: element) {
            int bucket = elem / 10;
        rangeFrequency[bucket] += 1;
        }
    }

    public String toString() {
        String result = "";

        result += "The average of these grades is : " + fmt.format(average);
        result += "The standard deviation of these grades is: " + fmt.format(deviation); 
        result += "Grade Range\tFrequency";

        for (int i = 0; i < rangeFrequency.length; ++i) {
            result += (i * 10 + "-" + (i * 10 + 9));
        result += ("\t\t" + rangeFrequency[i]);
        }
    }
}

下面的主要课程。

import java.io.*;
import java.lang.Math;
import java.util.*;
import java.text.DecimalFormat;


public class gradeSorter{

public static void main(String[] args) throws IOException {
    {

    intNode good = new intNode();

    }
    }
}

这些是我得到的错误

----jGRASP exec: javac -g gradeSorter.java

intNode.java:25: error: cannot find symbol
        while (numList.hasNextInt())
                      ^
  symbol:   method hasNextInt()
  location: variable numList of type NumberList
intNode.java:27: error: cannot find symbol
            element.add(numList.nextInt());
                               ^
  symbol:   method nextInt()
  location: variable numList of type NumberList
intNode.java:27: error: cannot find symbol
            element.add(numList.nextInt());
            ^
  symbol:   variable element
  location: class intNode
intNode.java:32: error: cannot find symbol
        for (int item : element){
                        ^
  symbol:   variable element
  location: class intNode
intNode.java:40: error: cannot find symbol
        average = sum / element.size();
                        ^
  symbol:   variable element
  location: class intNode
intNode.java:45: error: cannot find symbol
        for (int i = 0; i < element.size(); i++)
                            ^
  symbol:   variable element
  location: class intNode
intNode.java:47: error: cannot find symbol
            newnumber += Math.pow((element.get(i) - average),2);
                                   ^
  symbol:   variable element
  location: class intNode
intNode.java:49: error: cannot find symbol
        deviation = Math.sqrt(newnumber / (element.size()));
                                           ^
  symbol:   variable element
  location: class intNode
intNode.java:57: error: cannot find symbol
        for (int elem: element) {
                       ^
  symbol:   variable element
  location: class intNode
9 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
4

2 回答 2

3

编译器说“没有名为'元素'的变量”。你的代码说

//ArrayList<Integer> element = new ArrayList<Integer>();

也许你不应该评论它。

顺便说一句,您应该使用 java 编码约定。CamelCase 大写的类名。

于 2013-08-14T20:11:59.300 回答
0

通过一些修复以使您的事情以您想要的方式工作,这里是有效的代码:

但是,有很多事情需要解决,比如 Christian 提到的命名约定。此外,将所有变量都作为实例变量也不好。

package com.stackoverflow;

import java.text.DecimalFormat;
import java.util.ArrayList;

public class intNode
{

    double average;
    double deviation;
    int sum = 0;
    int number = 0;
    int newnumber = 0;
    int index;
    double frq;

    ArrayList<Integer> element = new ArrayList<Integer>();
    int[] rangeFrequency = new int[ 10 ];
    int[] numList = { 51, 52, 55, 57, 58, 61, 62, 63, 66, 66, 66, 70, 72, 73, 74, 75, 75, 77, 77,
        78, 79, 81, 82, 84, 86, 87, 88, 91, 94, 97 };
    DecimalFormat fmt = new DecimalFormat("0.000");

    // Start methods

    public void intAdd()
    {
        for (int num : numList)
        {
            element.add(num);
        }
    }

    public int sum()
    {
        for (int item : element)
        {
            sum += item;
            System.out.println(item);
        }
        return sum;
    }

    public double avg()
    {
        average = sum() / element.size();
        return average;
    }

    public double deviation()
    {
        for (int i = 0; i < element.size(); i++)
        {
            newnumber += Math.pow((element.get(i) - average), 2);
        }
        deviation = Math.sqrt(newnumber / (element.size()));

        return deviation;
    }

    public String frequency()
    {
        // int[] rangeFrequency = new int[10];

        for (int elem : element)
        {
            int bucket = elem / 10;
            rangeFrequency[ bucket ] += 1;
        }
        // TODO - Fixit. Return correct value.
        return null;
    }

    public String toString()
    {
        String result = "";

        result += "The average of these grades is : " + fmt.format(average);
        result += "The standard deviation of these grades is: " + fmt.format(deviation);
        result += "Grade Range\tFrequency";

        for (int i = 0; i < rangeFrequency.length; ++i)
        {
            result += (i * 10 + "-" + (i * 10 + 9));
            result += ("\t\t" + rangeFrequency[ i ]);
        }
        return result;
    }
}
于 2013-08-14T20:29:20.370 回答