大神求解。。为什么我的main方法结束后程序还不停止?
本帖最后由 a85524177 于 2012-11-22 12:54:25 编辑 如代码所示,输入0选择退出后,设置startSort值为false,while(startSort)循环应该结束,main()方法结束,但是在netbean中运行程序仍然不停止。。。奇怪的是调试的时候却会停止。。。小弟不才,有人知道为什么吗???苦恼。。。
public class Main extends JFrame {
/**
* @param args the command line arguments
*/
TextArea ta; //文本区域组件,用于输入数据
static File f = new File("data.txt"); //数据文件
static FileWriter fw;
static boolean startSort = true;
public static void main(String[] args) {
// TODO code application logic here
int param1=0, param2=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
fw = new FileWriter(f);
} catch (IOException e) {
e.printStackTrace();
}
if(args.length==0) { //如果命令行没有输入数据
System.out.println("命令行不含有参数,请选择其他方式导入数据");
System.out.println("************************************************");
System.out.println("1.手动输入 2.从文件夹导入 其他:退出");
System.out.println("************************************************");
try {
param1 = Integer.parseInt(br.readLine());
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
if(1==param1) {
new Main().launchFrame(); //选择手动输入后,弹出数据输入图形界面
}
else if(2==param1) {
}
else {
startSort = false;
}
}
else { //否则,将命令行输入的数据写入文件中
for(int i =0; i<args.length; i++) {
try {
fw.write(args[i]);
fw.write('\n');
fw.flush();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
while(startSort) {
BubbleSort bs =new BubbleSort();
InsertSort is = new InsertSort();
SelectSort ss = new SelectSort();
System.out.println("************************************************************");
System.out.println("请选择您想要排序的方法:1.冒泡排序 2.插入排序 3.选择排序 0.退出");
System.out.println("************************************************************");
try {
param2 = Integer.parseInt(br.readLine()); //读取参数
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
switch(param2) {
case 0:
startSort = false;
break;
case 1:
bs.getData(f);
bs.Sort();
System.out.println(bs);
break;
case 2:
is.getData(f);
is.Sort();
System.out.println(is);
break;
case 3:
ss.getData(f);
ss.Sort();
System.out.println(ss);
break;
}
}
}
public void launchFrame() { //弹出输入界面方法
this.setLocation(200, 200);
this.setSize(600, 400);
this.setTitle("排序性能测试小程序");
this.getContentPane().setBackground(Color.WHITE);
this.setLayout(null);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent arg0) {
System.exit(0);
}
});
ta = new TextArea();
ta.setBounds(100, 120, 400, 160); //设置输入框的位置和大小
JButton b;
b = new JButton("提交数据");
b.setBounds(250, 300, 100, 60); //设置按钮的位置和大小
b.addActionListener(new MyActionListener()); //为按钮添加事件监听器
this.add(ta); //添加组件
this.add(b); //添加组件
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String[] str = ta.getText().split(" "); //将字符串拆分以便写入数据文件
for(int i =0; i<str.length; i++) {
try {
fw.write(str[i]);
fw.write('\n');
fw.flush();
/*
* 将数据写入文件f中
*/
} catch (IOException e1) {
e1.printStackTrace();
}
}
System.out.println("数据写入完毕..."); //开始进入排序模块
JButton b = (JButton)e.getSource();
JFrame f = (JFrame)b.getTopLevelAncestor(); //得到当前JFrame容器
f.setVisible(false); //输入数据完毕,关闭输入界面
}
}
}
------解决方案--------------------
没人回吗??。。。。
[解决办法]
param1 = Integer.parseInt(br.readLine());
param2 = Integer.parseInt(br.readLine());
这两个语句下面你都加一个System.out.println()输出看看。
[解决办法]
输出为0,但程序仍在运行。。。
在debug时会结束while()循环然后结束程序。
[解决办法]
哦,突然好像想起来了。也许你因为的流还没关闭吧?服务端一直处于监听你输入的状态。
那个循环是没有运行的,否则会再次出现一次
System.out.println("************************************************************"); System.out.println("请选择您想要排序的方法:1.冒泡排序 2.插入排序 3.选择排序 0.退出"); System.out.println("************************************************************");
的。
[解决办法]
在case 0:中加入br.close()还是没用
[解决办法]
65 行有没有打印异常信息?
你的代码我copy过来就一行了
没法运行
你先不用Logger试试
