?? caishuzi.txt
字號:
首先給出完整代碼:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
/**
* @author Gary Chan
*
* 猜數(shù)字游戲
*/
public class GuessNumber {
public static void main(String[] args) {
// 新建一個隨機數(shù)產(chǎn)生器,然后生成一個0到99之間的整數(shù)
Random random = new Random();
int number = random.nextInt(100);
// 記錄玩家所猜測的數(shù)字
int guess = 0;
// 獲取控制臺輸入
BufferedReader input = new BufferedReader(
new InputStreamReader(System.in));
// 記錄玩家猜測的次數(shù)。
int counter = 0;
System.out.println("我心里有一個0到99之間的整數(shù),你猜是什么?");
do {
try {
// 獲取玩家的輸入。
guess = Integer.parseInt(input.readLine());
} catch (NumberFormatException e) {
// 如果玩家不是輸入一個合法的整數(shù),則讓他重新輸入。
System.out.println("請輸入一個0-99之間的整數(shù)!");
continue;
} catch (IOException e) {
System.out.println("程序發(fā)生異常錯誤將被關(guān)閉!");
e.printStackTrace();
}
// 對玩家的輸入進行判斷。
if (guess > number)
System.out.println("大了點,再猜!");
if (guess < number)
System.out.println("小了點,再試試!");
// 計數(shù)器增加一。
counter++;
} while (guess != number);
// 判斷成績。
switch (counter) {
case 1:
System.out.println("東漸……快來看上帝……");
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
System.out.println("這么快就猜對了,你很smart啊!");
break;
default:
System.out.println("猜了半天才猜出來,小同志,尚須努力啊!");
break;
}
System.out.println("Game Over!");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -