1. 程式人生 > >java小遊戲-猜數字

java小遊戲-猜數字

stat next equal pan () 1-1 main alt pre

程序設計思路

隨機產生一個1-100的隨機數字。
用戶輸入一個數字,然後做判斷並輸出判斷結果。
用戶根據判斷結果再次決定要輸入的數字。
重復上面兩步,直到用戶輸入正確或者用戶放棄輸入。

程序流程圖

技術分享

程序源代碼

import java.util.Random;

import javax.swing.JOptionPane;

public class GuessNmberGames {

public static void main(String[] args) {
    // TODO 自動生成的方法存根
    int question=new Random().nextInt(100) +1;
    System.out.println(question);
    while(true) {
        String answer=JOptionPane.showInputDialog("請輸入你的猜想:");
        if(answer==null)System.exit(0);
        if(answer.equals(""))System.exit(0);
        int e=Integer.parseInt(answer);
        if(e!=question)
        {
            if(e>question)
                JOptionPane.showMessageDialog(null, "大了");
            else 
                JOptionPane.showMessageDialog(null, "小了");
        }
        else
            {
            JOptionPane.showMessageDialog(null, "老鐵666");
            System.exit(0);
            
            }
            }
    }       
}

程序結果截圖

技術分享

技術分享

技術分享

java小遊戲-猜數字