程序应该从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.
答案 0 :(得分:3)
编译器说“没有可变的名为'element'”。你的代码说
//ArrayList<Integer> element = new ArrayList<Integer>();
可能你不应该评论它。
顺便说一下,你应该使用java编码约定。 CamelCase中的类名首先是大写。
答案 1 :(得分:0)
通过一些修复来让您的工作按照您想要的方式工作,以下是有效的代码:
然而,有许多事情需要解决,你应该像基督徒提到的那样考虑命名惯例。此外,将所有变量作为实例变量也不是很好。
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;
}
}