哪位大哥帮小弟看下java的一个小程序啊

来源:百度知道 编辑:UC知道 时间:2024/05/15 18:00:13
我的目的是在键盘输入一个a这个程序就停止运行
package Ttimer;

import java.io.IOException;
import java.util.Timer;

public class TimerTest {

public static void main(String[] args) throws IOException{
Timer timer = new Timer();
timer.schedule(new MyTask(), 1000, 2000);//在1秒后执行此任务,每次间隔2秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
int ch = 97;
while(ch == System.in.read()){//这个是用来停止此任务的,否则就一直循环执行此任务了
timer.cancel();
/*System.out.println("************");
int a = 111;
System.out.println(a);

//char ch = (char)System.in.read();

System.out.println("wwwwwwwwwwww");
System.out.println(a);
if(System.in.read()=='c')
{
timer.cancel();//使用这个方法退出任务
break;
}*/
}
}

static class MyTask extends java.util.TimerTask{
@Override
public void run() {
// TODO Auto-generated method stub

当第一次输入不是a的时候你的循环就停止了。
public class TimerTest {

public static void main(String[] args) throws IOException{
Timer timer = new Timer();
timer.schedule(new MyTask(), 1000, 2000);//在1秒b后执行此任务,每次间隔2秒,如果传递一个Data参数,就可以在某个固定的时间执行这个任务.
int ch = 97;
while(true){
if(ch == System.in.read()){
timer.cancel();
System.exit(0);
}
}
}

static class MyTask extends java.util.TimerTask{
public void run() {
System.out.println("________");
}
}
}

有什么问题呢?

你想表明一个什么问题呢?