|
首先我运行Windows下编译出来的.class文件(java Caculate)
终端提示为:
Exception in thread "main" java.lang.NoClassDefFoundError: Caculate$linkList
at Caculate.main(Caculate.java:20)
然后我把.java文件编译。
终端提示为:
Caculate.java:9: \u9519\u8bef\uff1aUnrecognized character for encoding 'UTF-8'\u3002
\u01bd JFrame frame = new JFrame("\u01bd\uffff\uffff\u01ab\uffff\uffff\uffff\uffff\u01bd\uffff\uffff\uffff\uffff\uffff\uffff");
^
1 error
我把中文都改成了英文后,再编译
终端提示为:
Caculate.java:159: 警告:An empty declaration is a deprecated feature that should not be used。
};
^
Caculate.java:121: 错误:Class ‘Caculate$linkList$5’ doesn't define the abstract method ‘java.lang.Object java.util.Iterator.next()’ from interface ‘java.util.Iterator’. This method must be defined or class ‘Caculate$linkList$5’ must be declared abstract。
return new Iterator(){
^
1 error, 1 warning
请各位大虾指点。
我的代码如下:
import javax.swing.*;
import java.util.Iterator;
import java.awt.*;
import java.awt.event.*;
class Caculate{
private static int numInputted = 0;
public static void main(String[] args) {
JFrame frame = new JFrame("平均偏差与平均方差"); //定义界面
final JLabel label = new JLabel("请输入数据:");
final JTextField inputLine = new JTextField(1);
label.setLabelFor(inputLine);
JPanel pane = new JPanel(); //加入一个JPanel用于加入下四个按键
JButton addButton = new JButton("添加数据");//加入数据
JButton button1 = new JButton("求平均偏差");//计算平均偏差
JButton button2 = new JButton("求平均方差");//计算平均方差
JButton retButton = new JButton("回零");//打输入清零
final linkList list = new linkList();//用于装数据的链表
pane.add(addButton);
addButton.addActionListener(new ActionListener(){//定义点击addButton的响应
public void actionPerformed(ActionEvent ae){
double data = 0;
try{
data = Double.parseDouble(inputLine.getText());
}catch(NumberFormatException e){
label.setText("输入格式不正确,请检查后继续输入:");
throw e;
}finally{
inputLine.setText("");
inputLine.grabFocus();
}
list.add(data);
numInputted ++ ;
label.setText("您已输入" + numInputted + "个数据,请输入数据:");
}
});
pane.add(button1);
button1.addActionListener(new ActionListener(){//定义点击button1的响应
public void actionPerformed(ActionEvent ae){
Iterator it = list.iterator();
double average = 0;
double sum = 0;
int amount = 0;
while (it.hasNext()){
sum += ((Double) it.next()).doubleValue();
amount ++ ;
}
if (amount == 0){
inputLine.grabFocus();
return;
}
average = sum / amount;
sum = 0;
it = list.iterator();
while (it.hasNext()){
sum += Math.abs(((Double) it.next()).doubleValue() - average);
}
average = sum / amount;
inputLine.setText(Double.toString(average));
inputLine.grabFocus();
}
});
pane.add(button2);
button2.addActionListener(new ActionListener(){//定义点击button2的响应
public void actionPerformed(ActionEvent ae){
Iterator it = list.iterator();
double average = 0;
double sum = 0;
int amount = 0;
while (it.hasNext()){
sum += ((Double) it.next()).doubleValue();
amount ++ ;
}
if (amount == 0){
inputLine.grabFocus();
return;
}
average = sum / amount;
sum = 0;
it = list.iterator();
while (it.hasNext()){
sum += Math.pow(Math.abs(((Double) it.next()).doubleValue() - average),2);
}
average = Math.sqrt(sum / amount);
inputLine.setText(Double.toString(average));
inputLine.grabFocus();
}
});
pane.add(retButton);
retButton.addActionListener(new ActionListener(){//定义点击retButton的响应
public void actionPerformed(ActionEvent ae){
list.dispose();
label.setText("请输入数据:");
numInputted = 0;
inputLine.setText("");
inputLine.grabFocus();
}
});
frame.getContentPane().add(pane,java.awt.BorderLayout.SOUTH);//加入各组件
frame.getContentPane().add(label,java.awt.BorderLayout.NORTH);
frame.getContentPane().add(inputLine,java.awt.BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static class linkList{ //定义链表类
private class linkListStructure{//链表结构类
private double element;
private linkListStructure next;
}
linkListStructure head = null;
linkListStructure current = null;
public Iterator iterator(){ //返回一个迭代器
return new Iterator(){//返回一个实现了Iterator接口的匿名类
private linkListStructure current = head;
private linkListStructure pre = null;
public boolean hasNext(){
if (current == null)
{
return false;
}
return true;
}
public Double next(){
pre = current;
current = current.next;
return pre.element;
}
public void remove(){
pre.next = current.next;
}
};
}
public void add(double data){//添加链表元素
linkListStructure temp;
temp = new linkListStructure();
if(head == null)
head = temp;
else
current.next = temp;
current = temp;
current.element = data;
}
public void dispose(){//清空链表
head = null;
current = null;
}
};
} |
|