鱼C论坛

 找回密码
 立即注册
查看: 170|回复: 1

哪里有错

[复制链接]
发表于 2024-4-6 17:06:48 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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[cnt][3];
                //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[i] = tempstr.split(" ");
                        i++;
                        tempstr="";
                    }
                    temp = reader.read();
                }
                //对数据进行排序
                double avg=Sort(str);
                String s="这些学生成绩的最大值是:";
                s+=str[cnt-1][2];
                s+=",最小值是:";
                s+=str[0][2];
                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[0]+" ").getBytes());
                    fileOutputStream.write((tmpstr[1]+" ").getBytes());
                    fileOutputStream.write((tmpstr[2]+"\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[j][2]) > Integer.parseInt(str[j + 1][2])) {
                    //完成两个字符串中成绩的排序
                    int temp = Integer.parseInt(str[j][2]);
                    str[j][2] = str[j + 1][2];
                    str[j + 1][2] = String.valueOf(temp);
                    //对学号和姓名同样交换
                    String _name = str[j][0];
                    str[j][0] = str[j + 1][0];
                    str[j + 1][0] = _name;

                    String _xuehao = str[j][1];
                    str[j][1] = str[j + 1][1];
                    str[j + 1][1] = _xuehao;

                }
            }

        }
        for(String []s:str)
        {
            avg+=Integer.parseInt(s[2]);
        }
        return avg/str.length;
    }
    public static void main(String[] args) throws IOException {
        new test05();
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-4-6 17:07:21 | 显示全部楼层
在代码中,有几处错误:

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自动关闭流、使用合适的变量名等。以下是修正后的代码:

  1. import javax.swing.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.io.*;

  5. public class Test05 extends JFrame implements ActionListener {
  6.     JLabel label_name, label_xuehao, label_grades;
  7.     JTextField field_name, field_xuehao, field_grades;
  8.     JTextArea area;
  9.     JButton button_add, button_baocun;
  10.     File file = null;
  11.     static int cnt = 0;

  12.     public Test05() {
  13.         init();
  14.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.         setVisible(true);
  16.         setBounds(400, 200, 700, 400);
  17.         setResizable(false);
  18.         setTitle("test05");
  19.     }

  20.     public void init() {
  21.         //布局设置
  22.         area = new JTextArea(19, 62);
  23.         area.setEditable(false);
  24.         label_grades = new JLabel("成绩:");
  25.         label_name = new JLabel("姓名:");
  26.         label_xuehao = new JLabel("学号");
  27.         field_grades = new JTextField(10);
  28.         field_name = new JTextField(10);
  29.         field_xuehao = new JTextField(10);
  30.         button_add = new JButton("添加学生");
  31.         button_baocun = new JButton("操作后保存");
  32.         add(new JScrollPane(area));
  33.         JPanel panel = new JPanel();
  34.         panel.add(label_name);
  35.         panel.add(field_name);
  36.         panel.add(label_xuehao);
  37.         panel.add(field_xuehao);
  38.         panel.add(label_grades);
  39.         panel.add(field_grades);
  40.         panel.add(button_add);
  41.         panel.add(button_baocun);
  42.         setContentPane(panel);
  43.         //添加监听器
  44.         button_baocun.addActionListener(this);
  45.         button_add.addActionListener(this);

  46.         file = new File("D:\\Java\\java学习\\学校作业\\test05.txt");
  47.         //判断该文件是否在硬盘中真实存在
  48.         if (!file.exists()) {
  49.             try {
  50.                 file.createNewFile();
  51.                 //System.out.println("创建文件成功了");
  52.             } catch (IOException e) {
  53.                 e.printStackTrace();
  54.                 //System.out.println("创建文件失败了");
  55.             }
  56.         }
  57.     }

  58.     @Override
  59.     public void actionPerformed(ActionEvent e) {
  60.         if (e.getSource() == button_add) {
  61.             String s = "";
  62.             if (field_name.getText().isEmpty())
  63.                 area.append("姓名不能为空\n");
  64.             else {
  65.                 s += field_name.getText();
  66.                 s += " ";
  67.             }
  68.             if (field_xuehao.getText().isEmpty())
  69.                 area.append("学号不能为空\n");
  70.             else {
  71.                 s += field_xuehao.getText();
  72.                 s += " ";
  73.             }
  74.             if (field_grades.getText().isEmpty())
  75.                 area.append("成绩不能为空\n");
  76.             else {
  77.                 s += field_grades.getText();
  78.                 s += "\n";
  79.             }
  80.             if (!field_name.getText().isEmpty() && !field_grades.getText().isEmpty() && !field_xuehao.getText().isEmpty()) {
  81.                 try {
  82.                     try (FileOutputStream fileOutputStream = new FileOutputStream("D:\\Java\\java学习\\学校作业\\test05.txt", true)) {
  83.                         fileOutputStream.write(s.getBytes());
  84.                     }
  85.                     try (FileOutputStream fileOutputStrea2 = new FileOutputStream("D:\\Java\\java学习\\学校作业\\test05.txt")) {
  86.                         fileOutputStrea2.write("".getBytes());
  87.                         fileOutputStrea2.flush();
  88.                     }
  89.                     field_grades.setText("");
  90.                     field_xuehao.setText("");
  91.                     field_name.setText("");
  92.                     JOptionPane.showMessageDialog(null, "添加成功", "关于", JOptionPane.INFORMATION_MESSAGE);
  93.                     area.append("第" + (cnt + 1) + "个学生:" + s);
  94.                     cnt++;
  95.                 } catch (IOException ex) {
  96.                     ex.printStackTrace();
  97.                 }
  98.             } else {
  99.                 JOptionPane.showMessageDialog(null, "添加失败", "关于", JOptionPane.WARNING_MESSAGE);
  100.             }
  101.         }
  102.         if (e.getSource() == button_baocun) {
  103.             try {
  104.                 try (Reader reader = new FileReader("D:\\Java\\java学习\\学校作业\\test05.txt")) {
  105.                     StringBuilder str = new StringBuilder();
  106.                     int temp = reader.read();
  107.                     int i = 0;
  108.                     String tempstr = "";
  109.                     while (temp != -1) {
  110.                         char ch = (char) temp;
  111.                         if (ch != '\n')
  112.                             tempstr += String.valueOf(ch);
  113.                         else {
  114.                             str.append(tempstr.split(" "));
  115.                             i++;
  116.                             tempstr = "";
  117.                         }
  118.                         temp = reader.read();
  119.                     }
  120.                     double avg = Sort(str.toString());
  121.                     String s = "这些学生成绩的最大值是:" + str + ",最小值是:" + str + ",平均值是:" + avg;
  122.                     JOptionPane.showMessageDialog(null, s, "计算结果", JOptionPane.INFORMATION_MESSAGE);
  123.                     try (FileOutputStream fileOutputStream = new FileOutputStream("E:\\Java\\HomeWork4\\src\\FileGui\\test2.txt", true)) {
  124.                         for (String tmpstr : str.toString().split(" ")) {
  125.                             fileOutputStream.write((tmpstr + " ").getBytes());
  126.                             fileOutputStream.write((tmpstr + " ").getBytes());
  127.                             fileOutputStream.write((tmpstr + "\n").getBytes());
  128.                         }
  129.                     }
  130.                 }
  131.             } catch (FileNotFoundException ex) {
  132.                 ex.printStackTrace();
  133.             } catch (IOException ioException) {
  134.                 ioException.printStackTrace();
  135.             }
  136.         }
  137.     }

  138.     //返回的是平均值
  139.     public double Sort(String str) {
  140.         String[] arr = str.split(" ");
  141.         for (int i = 0; i < arr.length - 1; i++) {
  142.             for (int j = 0; j < arr.length - 1 - i; j++) {
  143.                 if (Integer.parseInt(arr[j]) > Integer.parseInt(arr[j + 1])) {
  144.                     int temp = Integer.parseInt(arr[j]);
  145.                     arr[j] = arr[j + 1];
  146.                     arr[j + 1] = String.valueOf(temp);
  147.                 }
  148.             }
  149.         }
  150.         double sum = 0;
  151.         for (String s : arr) {
  152.             sum += Integer.parseInt(s);
  153.         }
  154.         return sum / arr.length;
  155.     }

  156.     public static void main(String args[]) throws IOException {
  157.         new Test05();
  158.     }
  159. }
复制代码


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 08:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表