⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defenchuli.java

📁 得分处理的程序
💻 JAVA
字号:
package softDesign;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;


class defenchuli extends JFrame {
     
	//文件路径
	
	static String sorcePath="sorce.txt";
	static String destPath="dest.txt";
	
	// 定义评委数量
	 final int PingWei=7;
     //定义选手最后得分
     double fScore[];

     //初始化界面
     defenchuli() {
		super("选手成绩表");
		Container cn = this.getContentPane();
		cn.add(new JLabel("选手成绩排序如下:"),BorderLayout.NORTH);
		final JTextArea textarea=new JTextArea(10,30);
		textarea.setWrapStyleWord(true);
		textarea.setEditable(false);
		cn.add(textarea);
		this.setSize(400, 300);
		this.setResizable(false);
		this.setVisible(true);

		final JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		final JMenuItem openSorceFile = new JMenuItem();
		openSorceFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Runtime rt = Runtime.getRuntime();
				try {				
				rt.exec("notepad.exe "+sorcePath);
				
				} catch (IOException ex) {
					
				System.out.println(ex.getMessage());
				
				}				
			}
		});
		openSorceFile.setText("打开原始文件");
		menuBar.add(openSorceFile);

		final JMenuItem openDestFile = new JMenuItem();
		openDestFile.setText("打开新文件");

		openDestFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Runtime rt = Runtime.getRuntime();
				try {
					
				rt.exec("notepad.exe "+destPath);
				
				} catch (IOException ex) {
					
				System.out.println(ex.getMessage());
				
				}				
			}
		});
		menuBar.add(openDestFile);	

		final JMenuItem redoShowScore = new JMenuItem();
		redoShowScore.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				defenchuli.this.showScore(sorcePath,destPath,textarea);
			}
		});
		redoShowScore.setText("重新载入");
		menuBar.add(redoShowScore);
	    
		//调用函数显示处理结果
		showScore(sorcePath,destPath,textarea);
		
	}
	
	
	//处理函数
	public  boolean showScore(String src, String des,JTextArea jt){
		jt.setText("");
      try{
    	     //打开文件
		try {
			File f = new File(src);
			if (!f.exists()) {
				System.out.println("请输入正确路径。");
				return false;
			}
			BufferedReader in = new BufferedReader(new FileReader(f));
			String str = "";
			String buf = "";
			
            //读入文件中的数据
			while ((str = in.readLine()) != null) {
				if (!str.equals(""))
				     buf += str+"\n";
			}
			
			in.close();
			//分割数据,初始化变量
			String every[]=buf.split("\n");
			fScore=new double[every.length];
			
			
			
			//格式化及赋值
			for (int i=0;i<every.length;i++){
			   every[i]=orderedString(every[i],i);
            
			}
			
			//排序输出结果
			for (int i=1;i<every.length;i++)
				arrayInsert(every,i);
		
			//打印标头 
			System.out.println("最后的评分结果:");
			System.out.println("参赛号    姓  名    最高分    最低分    累积分    最后得分");
			jt.append("参赛号    姓  名    最高分    最低分    累积分    最后得分\n");
			
			//存储结果
			f = new File(des);
			if (!f.exists()) 
				f.createNewFile();
				
			PrintWriter  out = new PrintWriter( new BufferedWriter(new FileWriter(f)) );
			out.println("最后的评分结果:");
			out.println("参赛号    姓  名    最高分    最低分    累积分    最后得分");
			
			//在界面上显示结果
			for (int i=0;i<every.length;i++){
				System.out.println(every[i]);
				jt.append(every[i]+"\n");
				out.println(every[i]);    
			}
			out.flush();
			out.close();

			return true;

		} catch (FileNotFoundException e) {
			System.out.println("没有找到文件,请正确输入路径。");
		}
      }catch(IOException event){
    	  System.out.println("读写文件错误。");
    	  
      }
			
		return false;
	}
	
	//折半查找法排序程序
	private void arrayInsert(String s[],int i){
		int left=0,right=i-1;
		double temp=fScore[i];
		String temps=s[i];
		while(left<=right){
			int middle=(left+right)/2;
            if (temp>fScore[middle])
            	right=middle-1;
            else
            	left=middle+1;
		}
		for(int k=i-1;k>=left;k--){
			fScore[k+1]=fScore[k];
			s[k+1]=s[k];
		}
		
		fScore[left]=temp;
		s[left]=temps;
		
		
	}
	
	//格式化结果函数
	public String orderedString(String s,int n){
		
		String str="";
		String splits[]=s.split(" ");
	
		double Max=Double.parseDouble( splits[2] );
		double Min=Max;
		double Sum=Max;
		for (int i=3;i<2+PingWei;i++){
			double temp=Double.parseDouble( splits[i] );
			if (temp>Max)
				Max=temp;
			if (temp<Min)
				Min=temp;
			
			Sum+=temp;
		}
		fScore[n]=(Sum-Max-Min)/5;
		
		str=formatStr(splits[0],4) + formatStr(splits[1],11) +  formatDouble(Max,9) + formatDouble(Min,9) + formatDouble(Sum,10) + formatDouble(fScore[n] ,11);

	
		return str;
	}
	
	//格式化字符串
	public String formatStr(String s,int len){
		String str="                "+s.trim();
		str=str.substring(str.length()-len, str.length());
		
		return str;
	}
	//格式化小数
	public String formatDouble(double s,int len){
		String str="               "+(String.valueOf( s )+"   ").substring(0, 4).trim();
		str=str.substring(str.length()-len, str.length());
		return str;
	}
	
	//主函数
	public static void main(String[] args) {
		
		while (true){
			String str=JOptionPane.showInputDialog(null, "输入存储成绩的文件路径", sorcePath);
			if (str==null)
				System.exit(0);
			if (!str.equals("")){
				sorcePath=str;
				break;
			}			
		}
		while (true){
			String str=JOptionPane.showInputDialog(null, "输入显示结果的文件路径", destPath);
			if (str==null)
				System.exit(0);
			if (!str.equals("")){
				destPath=str;
				break;
			}			
		}
		
		
		
		defenchuli sd=new defenchuli();
		sd.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -