大神求解。。为什么我的main方法结束后程序还不停止?

a85524177 2012-11-22 12:05:16
如代码所示,输入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); //输入数据完毕,关闭输入界面
}
}
}
...全文
835 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
a85524177 2012-11-22
  • 打赏
  • 举报
回复
没人回吗??。。。。
yongger520 2012-11-22
  • 打赏
  • 举报
回复
65 行有没有打印异常信息? 你的代码我copy过来就一行了 没法运行 你先不用Logger试试
a85524177 2012-11-22
  • 打赏
  • 举报
回复
引用 4 楼 AA5279AA 的回复:
哦,突然好像想起来了。也许你因为的流还没关闭吧?服务端一直处于监听你输入的状态。 那个循环是没有运行的,否则会再次出现一次 System.out.println("************************************************************"); System.out.println("请选择您想要……
在case 0:中加入br.close()还是没用
失落夏天 2012-11-22
  • 打赏
  • 举报
回复
哦,突然好像想起来了。也许你因为的流还没关闭吧?服务端一直处于监听你输入的状态。 那个循环是没有运行的,否则会再次出现一次 System.out.println("************************************************************"); System.out.println("请选择您想要排序的方法:1.冒泡排序 2.插入排序 3.选择排序 0.退出"); System.out.println("************************************************************"); 的。
a85524177 2012-11-22
  • 打赏
  • 举报
回复
引用 2 楼 AA5279AA 的回复:
param1 = Integer.parseInt(br.readLine());
param2 = Integer.parseInt(br.readLine());
这两个语句下面你都加一个System.out.println()输出看看。


输出为0,但程序仍在运行。。。
在debug时会结束while()循环然后结束程序。
失落夏天 2012-11-22
  • 打赏
  • 举报
回复
param1 = Integer.parseInt(br.readLine()); param2 = Integer.parseInt(br.readLine()); 这两个语句下面你都加一个System.out.println()输出看看。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧