import java.util.Scanner;
public class Thread3{
public static void main(String[] agrs){
Thread thread = new Thread(new Runnable(){
public void run(){
while(true){
System.out.print("\010" + "#"); try{
Thread.currentThread().sleep(50);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}); Scanner scanner = new Scanner(System.in);
System.out.print("请输入帐号:");
String name = scanner.next();
System.out.print("请输入密码:");
thread.start();
String code = scanner.next(); if(code.equals("123456")){
System.out.println("你好!" + name);
} else
System.out.println("密码错误!"); }
} 随便输入一个用户名 在提示输入密码 输入123456;程序通的过 //就是做个密码屏蔽!
但是thread 线程结束不了 不知道怎么改 请不要改变我程序的结构! 加上几个语句! 我知道可以的 但是我太菜了 做不出! 请高手指教!
public class Thread3{
public static void main(String[] agrs){
Thread thread = new Thread(new Runnable(){
public void run(){
while(true){
System.out.print("\010" + "#"); try{
Thread.currentThread().sleep(50);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}); Scanner scanner = new Scanner(System.in);
System.out.print("请输入帐号:");
String name = scanner.next();
System.out.print("请输入密码:");
thread.start();
String code = scanner.next(); if(code.equals("123456")){
System.out.println("你好!" + name);
} else
System.out.println("密码错误!"); }
} 随便输入一个用户名 在提示输入密码 输入123456;程序通的过 //就是做个密码屏蔽!
但是thread 线程结束不了 不知道怎么改 请不要改变我程序的结构! 加上几个语句! 我知道可以的 但是我太菜了 做不出! 请高手指教!
解决方案 »
- javafx怎么不显示dos窗口?
- javascript如何控制滚动条在最下方?
- 看看我的这个求素数的程序和书本后面的程序区别在哪儿?
- 用过CachedRowSetImpl 的高手请进
- 新手求助~~~~~~~~~出现错误!!
- 如何学习好java?
- applet的运行--新手的郁闷,快来帮忙!!!!在线等待!!!!!!!!!!
- ~~~ 变量类型转换问题,送分问题,急用! ~~~
- Jcreator Pro 初级问题
- java有没有留一个永远不用的字符,高手请进!!!
- 通过filechooser打开所选的文件,怎么把该文件的内容显示在JTable中?
- servle中取到的中文全是乱码, 路过帮忙看看,在线等
楼主【yangyongjie81】截止到2008-06-26 16:35:44的历史汇总数据(不包括此帖):
发帖数:3 发帖分:60
结贴数:3 结贴分:60
未结数:0 未结分:0
结贴率:100.00% 结分率:100.00%
敬礼!
在循环中设置一个开关,这个开关变量可以从外部改变,在需要退出时,从外部设置开关变量为false即可import java.util.Scanner;public class Thread3 {
static boolean running = false;//添加一个boolean变量用做开关
public static void main(String[] agrs) {
Thread thread = new Thread(new Runnable() {
public void run() {
while (running) {//使用变量开关
System.out.print("\010" + "#");
try {
Thread.currentThread().sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}); Scanner scanner = new Scanner(System.in);
System.out.print("请输入帐号:");
String name = scanner.next();
System.out.print("请输入密码:");
thread.start();
String code = scanner.next(); if (code.equals("123456")) {
System.out.println("你好!" + name);
}
else
System.out.println("密码错误!");
running = false;//在需要停止线程是,设置变量开关为false,线程会在本次循环后结束
}
}
public static void main(String[] agrs) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入帐号:");
String name = scanner.next();
System.out.print("请输入密码:");
Thread thread = new Thread(new Runnable(){
public void run() {
while (true) {
System.out.print("\010" + "#"); try {
Thread.sleep(5000);
} catch (InterruptedException e) {
System.out.println("hah");
e.printStackTrace();
return;//可以结束run
}
}
}
});
thread.start();
String code = scanner.next(); if (code.equals("123456")) {
System.out.println("你好!" + name);
thread.interrupt();//让目标线程阻塞
} else
System.out.println("密码错误!"); }
}