這篇文章主要介紹Java中怎么實(shí)現(xiàn)帶GUI界面的猜數(shù)字游戲,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
青龍網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)成立于2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運(yùn)維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
先導(dǎo)包
import java.util.*; import javax.swing.*;
再寫主方法
public static void main(String[] args) { }
新聲明一個Scanner和隨機(jī)數(shù)
public static void main(String[] args) { Scanner in = new Scanner(System.in); Random r = new Random(); }
讓UIManager爬取系統(tǒng)窗口樣式
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); }
新建一個int類型的變量存儲隨機(jī)數(shù)
int secret = r.nextInt(32) + 1;
寫入主程序
JOptionPane.showMessageDialog(null, "電腦隨機(jī)生成了一個1~32之間的數(shù),請猜出這個數(shù)", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); String number2 = (String) JOptionPane.showInputDialog(null, "請輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); int number = Integer.parseInt(number2); while (number != secret) { if (number > secret) { JOptionPane.showMessageDialog(null, "你猜的數(shù)大了,請繼續(xù)猜", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); number2 = (String) JOptionPane.showInputDialog(null, "請輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); number = Integer.parseInt(number2); } else { JOptionPane.showMessageDialog(null, "你猜的數(shù)小了,請繼續(xù)猜", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); number2 = (String) JOptionPane.showInputDialog(null, "請輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); number = Integer.parseInt(number2); } } JOptionPane.showMessageDialog(null, "恭喜你,你猜對了,電腦生成的隨機(jī)數(shù)是" + secret, "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE);
全部代碼
package com.demo05; import java.util.*; import javax.swing.*; public class MathDemo03 { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random r = new Random(); int secret = r.nextInt(32) + 1; try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } JOptionPane.showMessageDialog(null, "電腦隨機(jī)生成了一個1~32之間的數(shù),請猜出這個數(shù)", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); String number2 = (String) JOptionPane.showInputDialog(null, "請輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); int number = Integer.parseInt(number2); while (number != secret) { if (number > secret) { JOptionPane.showMessageDialog(null, "你猜的數(shù)大了,請繼續(xù)猜", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); number2 = (String) JOptionPane.showInputDialog(null, "請輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); number = Integer.parseInt(number2); } else { JOptionPane.showMessageDialog(null, "你猜的數(shù)小了,請繼續(xù)猜", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); number2 = (String) JOptionPane.showInputDialog(null, "請輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); number = Integer.parseInt(number2); } } JOptionPane.showMessageDialog(null, "恭喜你,你猜對了,電腦生成的隨機(jī)數(shù)是" + secret, "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); } }
以上是“Java中怎么實(shí)現(xiàn)帶GUI界面的猜數(shù)字游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享題目:Java中怎么實(shí)現(xiàn)帶GUI界面的猜數(shù)字游戲
轉(zhuǎn)載來源:http://vcdvsql.cn/article10/pdhogo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站設(shè)計公司、全網(wǎng)營銷推廣、小程序開發(fā)、網(wǎng)站改版、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)