哪里有错
import javax.swing.*;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class test05 extends JFrame implements ActionListener {
JLabel label_name,label_xuehao,label_grades;
JTextField field_name,field_xuehao,field_grades;
JTextArea area;
JButton button_add,button_baocun;
File file=null;
static int cnt=0;
public test05()
{
init();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(400,200,700,400);
setResizable(false);
setTitle("test05");
}
public void init()
{
//布局设置
area=new JTextArea(19,62);
area.setEditable(false);
label_grades=new JLabel("成绩:");
label_name=new JLabel("姓名:");
label_xuehao=new JLabel("学号");
field_grades=new JTextField(10);
field_name=new JTextField(10);
field_xuehao=new JTextField(10);
button_add=new JButton("添加学生");
button_baocun=new JButton("操作后保存");
add(new JScrollPane(area));
JPanel panel=new JPanel();
panel.add(label_name);
panel.add(field_name);
panel.add(label_xuehao);
panel.add(field_xuehao);
panel.add(label_grades);
panel.add(field_grades);
panel.add(button_add);
panel.add(button_baocun);
setContentPane(panel);
add(new JScrollPane(area));
//添加监听器
button_baocun.addActionListener(this);
button_add.addActionListener(this);
file=new File("D:\\Java\\java学习\\学校作业\\test05.txt");
//判断该文件是否在硬盘中真实存在
if(!file.exists()){
try {
file.createNewFile();
//System.out.println("创建文件成功了");
} catch (IOException e) {
e.printStackTrace();
//System.out.println("创建文件失败了");
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button_add)
{
String s="";
if(field_name.getText().isEmpty())
area.append("姓名不能为空\n");
else
{
s+=field_name.getText();
s+=" ";
}
if(field_xuehao.getText().isEmpty())
area.append("学号不能为空\n");
else {
s += field_xuehao.getText();
s += " ";
}
if(field_grades.getText().isEmpty())
area.append("成绩不能为空\n");
else
{
s+=field_grades.getText();
s+="\n";
}
if(!field_name.getText().isEmpty() && !field_grades.getText().isEmpty() && !field_xuehao.getText().isEmpty()) {
try {
FileOutputStream fileOutputStream = new FileOutputStream("D:\\Java\\java学习\\学校作业\\test05.txt", true);
fileOutputStream.write(s.getBytes());
FileOutputStream fileOutputStrea2 = new FileOutputStream("D:\\Java\\java学习\\学校作业\\test05.txt");
fileOutputStrea2.write("".getBytes());
fileOutputStrea2.flush();
fileOutputStrea2.close();
field_grades.setText("");
field_xuehao.setText("");
field_name.setText("");
JOptionPane.showMessageDialog(null, "添加成功", "关于", JOptionPane.INFORMATION_MESSAGE);
area.append("第" + (cnt + 1) + "个学生:" + s);
cnt++;
fileOutputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
else{
JOptionPane.showMessageDialog(null, "添加失败", "关于", JOptionPane.WARNING_MESSAGE);
}
}
if(e.getSource()==button_baocun)
{
try {
Reader reader=new FileReader("D:\\Java\\java学习\\学校作业\\test05.txt");
String [][]str=new String;
//System.out.println(cnt);
int temp=reader.read();
int i=0;
String tempstr ="";
while (temp != -1) {
//因为读取是按照字符一个个读取,所以i很容易变得很大
char ch = (char) temp;
if(ch!='\n')
tempstr +=String.valueOf(ch);
else {
str = tempstr.split(" ");
i++;
tempstr="";
}
temp = reader.read();
}
//对数据进行排序
double avg=Sort(str);
String s="这些学生成绩的最大值是:";
s+=str;
s+=",最小值是:";
s+=str;
s+=",平均值是:";
s+=avg;
JOptionPane.showMessageDialog(null,s,"计算结果",JOptionPane.INFORMATION_MESSAGE);
FileOutputStream fileOutputStream = new FileOutputStream("E:\\Java\\HomeWork4\\src\\FileGui\\test2.txt", true);
for(String []tmpstr:str) {
fileOutputStream.write((tmpstr+" ").getBytes());
fileOutputStream.write((tmpstr+" ").getBytes());
fileOutputStream.write((tmpstr+"\n").getBytes());
}
fileOutputStream.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
//返回的是平均值
static public double Sort(String[][]str) {
int avg=0;
for (int i = 0; i < str.length - 1; i++) {
for (int j = 0; j < str.length - 1 - i; j++) {
if (Integer.parseInt(str) > Integer.parseInt(str)) {
//完成两个字符串中成绩的排序
int temp = Integer.parseInt(str);
str = str;
str = String.valueOf(temp);
//对学号和姓名同样交换
String _name = str;
str = str;
str = _name;
String _xuehao = str;
str = str;
str = _xuehao;
}
}
}
for(String []s:str)
{
avg+=Integer.parseInt(s);
}
return avg/str.length;
}
public static void main(String[] args) throws IOException {
new test05();
}
} 在代码中,有几处错误:
1. 在`actionPerformed`方法中,`String str=new String;`应该是`String str = "";`,缺少了括号。
2. 在`actionPerformed`方法中,`str = tempstr.split(" ");`应该是`str = tempstr.split(" ");`,缺少了括号。
3. 在`Sort`方法中,`static public double Sort(Stringstr)`应该是`static public double Sort(String[] str)`,缺少了空格。
另外,为了保证代码的可读性和可维护性,建议对代码进行一些优化和改进,比如使用try-with-resources自动关闭流、使用合适的变量名等。以下是修正后的代码:
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class Test05 extends JFrame implements ActionListener {
JLabel label_name, label_xuehao, label_grades;
JTextField field_name, field_xuehao, field_grades;
JTextArea area;
JButton button_add, button_baocun;
File file = null;
static int cnt = 0;
public Test05() {
init();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(400, 200, 700, 400);
setResizable(false);
setTitle("test05");
}
public void init() {
//布局设置
area = new JTextArea(19, 62);
area.setEditable(false);
label_grades = new JLabel("成绩:");
label_name = new JLabel("姓名:");
label_xuehao = new JLabel("学号");
field_grades = new JTextField(10);
field_name = new JTextField(10);
field_xuehao = new JTextField(10);
button_add = new JButton("添加学生");
button_baocun = new JButton("操作后保存");
add(new JScrollPane(area));
JPanel panel = new JPanel();
panel.add(label_name);
panel.add(field_name);
panel.add(label_xuehao);
panel.add(field_xuehao);
panel.add(label_grades);
panel.add(field_grades);
panel.add(button_add);
panel.add(button_baocun);
setContentPane(panel);
//添加监听器
button_baocun.addActionListener(this);
button_add.addActionListener(this);
file = new File("D:\\Java\\java学习\\学校作业\\test05.txt");
//判断该文件是否在硬盘中真实存在
if (!file.exists()) {
try {
file.createNewFile();
//System.out.println("创建文件成功了");
} catch (IOException e) {
e.printStackTrace();
//System.out.println("创建文件失败了");
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button_add) {
String s = "";
if (field_name.getText().isEmpty())
area.append("姓名不能为空\n");
else {
s += field_name.getText();
s += " ";
}
if (field_xuehao.getText().isEmpty())
area.append("学号不能为空\n");
else {
s += field_xuehao.getText();
s += " ";
}
if (field_grades.getText().isEmpty())
area.append("成绩不能为空\n");
else {
s += field_grades.getText();
s += "\n";
}
if (!field_name.getText().isEmpty() && !field_grades.getText().isEmpty() && !field_xuehao.getText().isEmpty()) {
try {
try (FileOutputStream fileOutputStream = new FileOutputStream("D:\\Java\\java学习\\学校作业\\test05.txt", true)) {
fileOutputStream.write(s.getBytes());
}
try (FileOutputStream fileOutputStrea2 = new FileOutputStream("D:\\Java\\java学习\\学校作业\\test05.txt")) {
fileOutputStrea2.write("".getBytes());
fileOutputStrea2.flush();
}
field_grades.setText("");
field_xuehao.setText("");
field_name.setText("");
JOptionPane.showMessageDialog(null, "添加成功", "关于", JOptionPane.INFORMATION_MESSAGE);
area.append("第" + (cnt + 1) + "个学生:" + s);
cnt++;
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "添加失败", "关于", JOptionPane.WARNING_MESSAGE);
}
}
if (e.getSource() == button_baocun) {
try {
try (Reader reader = new FileReader("D:\\Java\\java学习\\学校作业\\test05.txt")) {
StringBuilder str = new StringBuilder();
int temp = reader.read();
int i = 0;
String tempstr = "";
while (temp != -1) {
char ch = (char) temp;
if (ch != '\n')
tempstr += String.valueOf(ch);
else {
str.append(tempstr.split(" "));
i++;
tempstr = "";
}
temp = reader.read();
}
double avg = Sort(str.toString());
String s = "这些学生成绩的最大值是:" + str + ",最小值是:" + str + ",平均值是:" + avg;
JOptionPane.showMessageDialog(null, s, "计算结果", JOptionPane.INFORMATION_MESSAGE);
try (FileOutputStream fileOutputStream = new FileOutputStream("E:\\Java\\HomeWork4\\src\\FileGui\\test2.txt", true)) {
for (String tmpstr : str.toString().split(" ")) {
fileOutputStream.write((tmpstr + " ").getBytes());
fileOutputStream.write((tmpstr + " ").getBytes());
fileOutputStream.write((tmpstr + "\n").getBytes());
}
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
//返回的是平均值
public double Sort(String str) {
String[] arr = str.split(" ");
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (Integer.parseInt(arr) > Integer.parseInt(arr)) {
int temp = Integer.parseInt(arr);
arr = arr;
arr = String.valueOf(temp);
}
}
}
double sum = 0;
for (String s : arr) {
sum += Integer.parseInt(s);
}
return sum / arr.length;
}
public static void main(String args[]) throws IOException {
new Test05();
}
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]