?? 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 + -