Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xià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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:SpringBoot Session共享實(shí)現(xiàn)圖解
欄 目:Java
本文標(biāo)題:Java的帶GUI界面猜數(shù)字游戲的實(shí)現(xiàn)示例
本文地址:http://mengdiqiu.com.cn/a1/Java/8791.html
您可能感興趣的文章
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10淺談Java中真的只有值傳遞么
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10如何解決線程太多導(dǎo)致java socket連接池出現(xiàn)的問題


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動(dòng)態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動(dòng)態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery