java小遊戲-猜數字

來源:互聯網
上載者:User

標籤:als   輸出   put   guess   輸入   print   自動   input   產生   

程式設計思路

隨機產生一個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小遊戲-猜數字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.