這篇文章主要介紹Java中怎么實(shí)現(xiàn)帶GUI界面的猜數(shù)字游戲,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
青龍網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)成立于2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
先導(dǎo)包
import java.util.*; import javax.swing.*;
再寫主方法
public static void main(String[] args) { }
新聲明一個(gè)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(); }
新建一個(gè)int類型的變量存儲(chǔ)隨機(jī)數(shù)
int secret = r.nextInt(32) + 1;
寫入主程序
JOptionPane.showMessageDialog(null, "電腦隨機(jī)生成了一個(gè)1~32之間的數(shù),請(qǐng)猜出這個(gè)數(shù)", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); String number2 = (String) JOptionPane.showInputDialog(null, "請(qǐng)輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); int number = Integer.parseInt(number2); while (number != secret) { if (number > secret) { JOptionPane.showMessageDialog(null, "你猜的數(shù)大了,請(qǐng)繼續(xù)猜", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); number2 = (String) JOptionPane.showInputDialog(null, "請(qǐng)輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); number = Integer.parseInt(number2); } else { JOptionPane.showMessageDialog(null, "你猜的數(shù)小了,請(qǐng)繼續(xù)猜", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); number2 = (String) JOptionPane.showInputDialog(null, "請(qǐng)輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); number = Integer.parseInt(number2); } } JOptionPane.showMessageDialog(null, "恭喜你,你猜對(duì)了,電腦生成的隨機(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ī)生成了一個(gè)1~32之間的數(shù),請(qǐng)猜出這個(gè)數(shù)", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); String number2 = (String) JOptionPane.showInputDialog(null, "請(qǐng)輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); int number = Integer.parseInt(number2); while (number != secret) { if (number > secret) { JOptionPane.showMessageDialog(null, "你猜的數(shù)大了,請(qǐng)繼續(xù)猜", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); number2 = (String) JOptionPane.showInputDialog(null, "請(qǐng)輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); number = Integer.parseInt(number2); } else { JOptionPane.showMessageDialog(null, "你猜的數(shù)小了,請(qǐng)繼續(xù)猜", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); number2 = (String) JOptionPane.showInputDialog(null, "請(qǐng)輸入想猜的數(shù):", "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE, null, null, ""); number = Integer.parseInt(number2); } } JOptionPane.showMessageDialog(null, "恭喜你,你猜對(duì)了,電腦生成的隨機(jī)數(shù)是" + secret, "猜數(shù)字游戲", JOptionPane.PLAIN_MESSAGE); } }
以上是“Java中怎么實(shí)現(xiàn)帶GUI界面的猜數(shù)字游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!