带有 StackOverflowError 的 Swing 计时器和 ActionListener
Swing Timer and ActionListener with StackOverflowError
我得到了一个我尝试了很长时间的作业,甚至向教授询问了它,但仍然不明白这段代码出了什么问题。我完全不知道如何初始化时间(在 SoilTempData 构造函数中),也不确定错误有什么问题,所以我不知道如何修复它。由于保留给定代码结构的问题,我也不能太多更改代码
[这是给定的图表][1]
package Iot;
import java.util.ArrayList;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SoilTempData implements ActionListener{
private final int REPORT_ROUND = 1000/100;
ArrayList<Integer> data;
private int round;
private int nextPrintIndex;
private Timer time;
private SoilTempIoT iot;
public SoilTempData(SoilTempIoT iot){
data = new ArrayList<>(10);
round = 0;
nextPrintIndex = 0;
time = new Timer(100, new SoilTempData(iot));
iot = new SoilTempIoT();
}
public void start() {
time.start();
}
public void actionPerformed(ActionEvent e){
if(!(round == REPORT_ROUND))
time.start();
else
printStat();
}
public void printStat() {
int min = data.get(nextPrintIndex);
int max = data.get(nextPrintIndex);
int sum = 0;
double average = 0.0;
double sd = 0.0;
System.out.println("********** Report Current Data: **********");
System.out.print("New set of data: ");
for(int i = nextPrintIndex ; i < data.size() ; i++) {
System.out.print(data.get(i) +", ");
sum += data.get(i);
round++;
}
average = sum/data.size();
for(int i = nextPrintIndex ; i < data.size() ; i++) {
sd += Math.pow((data.get(i) - average),2);
}
System.out.printf("\nTotal records: %d\n",REPORT_ROUND*round);
System.out.println("Statistics:");
if(!data.isEmpty()) {
System.out.printf("Min = %.2f\nMax = %.2f\nAverage = %.2f\nSD = %.2f\n",min,max,average,sd);
}
else
System.out.println("No data to report!");
nextPrintIndex = REPORT_ROUND*round;
}
}
这是 SoilTempIoT
package Iot;
import java.awt.event.ActionListener;
import java.util.Random;
public class SoilTempIoT {
public static final int MIN_TEMP = 10;
public static final int MAX_TEMP = 60;
private int temp;
private Random rand;
public SoilTempIoT() {
rand = new Random();
temp = rand.nextInt(50) + 10;
}
public int getTemp() {
int i = rand.nextInt(21) - 10;
return temp+i;
}
}
和 Main class(由于问题无法更改任何内容,但万一有人想看一下)
package Iot;
import javax.swing.JOptionPane;
public class SoilTempDataTest {
public static void main(String[] args) {
SoilTempData sensor = new SoilTempData(new SoilTempIoT());
sensor.printStat();
sensor.start();
JOptionPane.showMessageDialog(null, "Quit?");
System.exit(0);
}
}
如果我的问题有任何不好的地方请告诉我,我会加班改进。
编辑:我还需要这样做,以便 SoilTempData 从 SoilTempIoT 100ms 读取数据,并且每 1000ms 打印一次 Stat。
[1]: https://i.stack.imgur.com/VDefv.png
public SoilTempData(SoilTempIoT iot){
data = new ArrayList<>(10);
round = 0;
nextPrintIndex = 0;
time = new Timer(100, new SoilTempData(iot));
iot = new SoilTempIoT();
}
当您初始化 time
时,您创建了另一个实例。调用相同的构造函数,初始化 time
,创建另一个实例,等等
只需使用time = new Timer(100, this);
我得到了一个我尝试了很长时间的作业,甚至向教授询问了它,但仍然不明白这段代码出了什么问题。我完全不知道如何初始化时间(在 SoilTempData 构造函数中),也不确定错误有什么问题,所以我不知道如何修复它。由于保留给定代码结构的问题,我也不能太多更改代码 [这是给定的图表][1]
package Iot;
import java.util.ArrayList;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SoilTempData implements ActionListener{
private final int REPORT_ROUND = 1000/100;
ArrayList<Integer> data;
private int round;
private int nextPrintIndex;
private Timer time;
private SoilTempIoT iot;
public SoilTempData(SoilTempIoT iot){
data = new ArrayList<>(10);
round = 0;
nextPrintIndex = 0;
time = new Timer(100, new SoilTempData(iot));
iot = new SoilTempIoT();
}
public void start() {
time.start();
}
public void actionPerformed(ActionEvent e){
if(!(round == REPORT_ROUND))
time.start();
else
printStat();
}
public void printStat() {
int min = data.get(nextPrintIndex);
int max = data.get(nextPrintIndex);
int sum = 0;
double average = 0.0;
double sd = 0.0;
System.out.println("********** Report Current Data: **********");
System.out.print("New set of data: ");
for(int i = nextPrintIndex ; i < data.size() ; i++) {
System.out.print(data.get(i) +", ");
sum += data.get(i);
round++;
}
average = sum/data.size();
for(int i = nextPrintIndex ; i < data.size() ; i++) {
sd += Math.pow((data.get(i) - average),2);
}
System.out.printf("\nTotal records: %d\n",REPORT_ROUND*round);
System.out.println("Statistics:");
if(!data.isEmpty()) {
System.out.printf("Min = %.2f\nMax = %.2f\nAverage = %.2f\nSD = %.2f\n",min,max,average,sd);
}
else
System.out.println("No data to report!");
nextPrintIndex = REPORT_ROUND*round;
}
}
这是 SoilTempIoT
package Iot;
import java.awt.event.ActionListener;
import java.util.Random;
public class SoilTempIoT {
public static final int MIN_TEMP = 10;
public static final int MAX_TEMP = 60;
private int temp;
private Random rand;
public SoilTempIoT() {
rand = new Random();
temp = rand.nextInt(50) + 10;
}
public int getTemp() {
int i = rand.nextInt(21) - 10;
return temp+i;
}
}
和 Main class(由于问题无法更改任何内容,但万一有人想看一下)
package Iot;
import javax.swing.JOptionPane;
public class SoilTempDataTest {
public static void main(String[] args) {
SoilTempData sensor = new SoilTempData(new SoilTempIoT());
sensor.printStat();
sensor.start();
JOptionPane.showMessageDialog(null, "Quit?");
System.exit(0);
}
}
如果我的问题有任何不好的地方请告诉我,我会加班改进。
编辑:我还需要这样做,以便 SoilTempData 从 SoilTempIoT 100ms 读取数据,并且每 1000ms 打印一次 Stat。 [1]: https://i.stack.imgur.com/VDefv.png
public SoilTempData(SoilTempIoT iot){ data = new ArrayList<>(10); round = 0; nextPrintIndex = 0; time = new Timer(100, new SoilTempData(iot)); iot = new SoilTempIoT(); }
当您初始化 time
时,您创建了另一个实例。调用相同的构造函数,初始化 time
,创建另一个实例,等等
只需使用time = new Timer(100, this);