史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   出題-猜數字遊戲 (http://forum.slime.com.tw/thread195311.html)

飛鳥 2007-01-06 02:19 PM

出題-猜數字遊戲
 
像一些電視節目的遊戲

執行畫面:
引用:

範圍:1~100
請猜數字:70

範圍:1~69
請猜數字:55

範圍:1~54
請猜數字:23

範圍:24~54
請猜數字:45

範圍:46~54
請猜數字:51

猜對![51]


snoopy 2007-01-06 03:24 PM

語法:

import java.io.*;

public class Slime {
        public static void main(String[] args) throws IOException {
                BufferedReader bfr = new BufferedReader(
                                new InputStreamReader(System.in));
                int guess = 0, ans, min = 1, max = 100;
                ans = (int) (Math.random() * 100);
                while (guess != ans) {
                        System.out.print("請輸入數字,範圍在 " + min + " 和 " + max + " 之間 : ");
                        guess = Integer.parseInt(bfr.readLine());
                        if (guess > ans)
                                max = guess;
                        else if (guess < ans)
                                min = guess;
                        else
                                break;
                }
                System.out.println("答對了!");
        }
}

語法:

請輸入數字,範圍在 1 和 100 之間 : 50
請輸入數字,範圍在 50 和 100 之間 : 75
請輸入數字,範圍在 75 和 100 之間 : 85
請輸入數字,範圍在 75 和 85 之間 : 80
請輸入數字,範圍在 75 和 80 之間 : 77
請輸入數字,範圍在 77 和 80 之間 : 78
答對了!



所有時間均為台北時間。現在的時間是 09:38 PM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2024, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1