📄 hoho.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class Ball extends JFrame implements ActionListener {
JButton OKBtn = new JButton("OK", new ImageIcon("pic.gif"));
JTextArea text = new JTextArea("",20,25);
Container mainPane=getContentPane();
Ball() {
//初始化界面
setVisible(true);
setBounds(100,100,300,300);
mainPane.setLayout(new GridLayout(1,2));
OKBtn.setHorizontalTextPosition(AbstractButton.CENTER);
OKBtn.setVerticalTextPosition(AbstractButton.BOTTOM);
JScrollPane scrollpane = new JScrollPane(text);
mainPane.add(scrollpane);
mainPane.add(OKBtn);
//响应鼠标事件
OKBtn.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==OKBtn) {
int rowCnt[]=new int[2];
do {
rowCnt= union();
} while(rowCnt[0]!=rowCnt[1]);
}
}
//数列合并方法
public int[] union(){
int cntTemp[]=new int[2];
String strTemp="";
//确定文件中数列的件数
try {
File file = new File("test.txt");
FileReader infile = new FileReader(file);
BufferedReader in = new BufferedReader(infile);
while((strTemp=in.readLine())!=null) {
cntTemp[0]++;
}
infile.close();
in.close();
} catch(Exception e01) {}
String temp[]=new String[cntTemp[0]];
int cnt=0;
//将文件中的数列读到字符串数组中
try {
File file = new File("test.txt");
FileReader infile = new FileReader(file);
BufferedReader in = new BufferedReader(infile);
while((strTemp=in.readLine())!=null) {
temp[cnt++]=strTemp;
}
infile.close();
in.close();
} catch(Exception e02) {}
for(int i=0;i<cnt;i++){
for(int j=i+1;j<cnt;j++){
if(!temp[i].equals("BeThrowed")) {
//使用上面讲述的算法进行合并
long varTemp1=0;
long varTemp2=0;
long varTemp3=0;
long length=0;
long mod=1;
varTemp1=Long.parseLong(temp[i]);
varTemp2=Long.parseLong(temp[j]);
varTemp3=varTemp1-varTemp2;
if(varTemp3<0){
varTemp3=varTemp3*(-1);
}
length=String.valueOf(varTemp3).length();
for(long k=0;k<length-1;k++) {
mod*=10;
}
if(varTemp3%mod==0){
//符合合并条件的话,修改其中一个,并丢掉另一个
long n=0;
n=(varTemp2%(mod*10))/mod;
varTemp1+=n*mod;
temp[i]=String.valueOf(varTemp1);
temp[j]="BeThrowed";
}
}
}
}
//将结果输出到文件中去(被作丢掉标记的不输出)
try {
String str="";
File files = new File("test.txt");
FileWriter tofile = new FileWriter(files);
BufferedWriter out= new BufferedWriter(tofile);
for(int i=0;i<cnt;i++) {
if(!temp[i].equals("BeThrowed")){
str+=temp[i]+"\n";
cntTemp[1]++;
}
}
out.write(str, 0,str.length());
out.flush();
tofile.close();
out.close();
}catch(Exception e03){}
//打印文件操作前后的数列条数
text.append("*******************************************\n");
text.append("in : "+cntTemp[0]+"\n");
text.append("out : "+cntTemp[1]+"\n");
text.append("*******************************************\n");
return cntTemp;
}
}
public class Hoho {
public static void main(String args[]){
Ball win= new Ball();
win.pack();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -