プログラムは 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.