是的,在第二个return;前加上System.exit(0);语句,就可以自动退出了。但是加上System.exit(0);语句,第二个return;就没用了。说的简单点,第二个return;就成了“不可达语句”。因为执行了System.exit(0);该程序就退出了,不会执行return. //以下是部分完整代码: import java.lang.Runnable; //之后未改的一部分略去了“输入内容已经达到长度限制,请删减” class Number implements Runnable { int realNumber, guessNumber, min = 0, max = 100, message; final int SMALLER = -1, LARGER = 1, SUCCESS = 8; Thread giveNumberThread, guessNumberThread; Number() { giveNumberThread = new Thread(this); guessNumberThread = new Thread(this); } @Override public synchronized void run() { // TODO Auto-generated method stub for (int count = 1; true; count++) { if (Thread.currentThread() == giveNumberThread) { if (count == 1) { realNumber = (int) (Math.random() * 100) + 1; System.out.println("随机给你一个数为" + realNumber + ",猜猜是多少"); } else { if (realNumber > guessNumber) { message = SMALLER; System.out.println("你猜小了"); } else if (realNumber < guessNumber) { message = LARGER; System.out.println("你猜大了"); } else { message = SUCCESS; System.out.println("恭喜,你猜对了"); } } try { notify(); wait();//Thread.sleep(1500); } catch (Exception e) { } } else if (Thread.currentThread() == guessNumberThread) { if (count == 1) { guessNumber = (min + max) / 2; System.out.println("我第" + count + "次猜这个数:" + guessNumber); } else { if (message == SMALLER) { min = guessNumber; guessNumber = (min + max) / 2; System.out.println("我第" + count + "次猜这个数是" + guessNumber); } else if (message == LARGER) { max = guessNumber; guessNumber = (min + max) / 2; System.out.println("我第" + count + "次猜这个数是" + guessNumber); } else if (message == SUCCESS) { System.out.println("我成功了"); System.exit(0);//return; } } try { notify(); wait();//Thread.sleep(); } catch (Exception e) { } } } } }
java多线程实现猜数字_java多线程猜数字问题package test;/*** 用两个线程来玩猜数字游戏,第一个线程负责随机给出1-100之...
最新推荐文章于 2023-02-10 23:01:03 发布