Java猜数字游戏(带GUI)

先导包

import java.util.*;
import javax.swing.*;

image
再写主方法

public static void main(String[] args) {
}

image
新声明一个Scanner和随机数

public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	Random r = new Random();
}

image
让UIManager爬取系统窗口样式

try {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
    e.printStackTrace();
}

image
新建一个int类型的变量存储随机数

int secret = r.nextInt(32) + 1;

写入主程序

JOptionPane.showMessageDialog(null, "电脑随机生成了一个1~32之间的数,请猜出这个数", "猜数字游戏", JOptionPane.PLAIN_MESSAGE);
String number2 = (String) JOptionPane.showInputDialog(null, "请输入想猜的数:", "猜数字游戏", JOptionPane.PLAIN_MESSAGE, null, null, "");
int number = Integer.parseInt(number2);
while (number != secret) {
    if (number > secret) {
        JOptionPane.showMessageDialog(null, "你猜的数大了,请继续猜", "猜数字游戏", JOptionPane.PLAIN_MESSAGE);
        number2 = (String) JOptionPane.showInputDialog(null, "请输入想猜的数:", "猜数字游戏", JOptionPane.PLAIN_MESSAGE, null, null, "");
        number = Integer.parseInt(number2);
    } else {
        JOptionPane.showMessageDialog(null, "你猜的数小了,请继续猜", "猜数字游戏", JOptionPane.PLAIN_MESSAGE);
        number2 = (String) JOptionPane.showInputDialog(null, "请输入想猜的数:", "猜数字游戏", JOptionPane.PLAIN_MESSAGE, null, null, "");
        number = Integer.parseInt(number2);
    }
}
JOptionPane.showMessageDialog(null, "恭喜你,你猜对了,电脑生成的随机数是" + secret, "猜数字游戏", 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, "电脑随机生成了一个1~32之间的数,请猜出这个数", "猜数字游戏", JOptionPane.PLAIN_MESSAGE);
        String number2 = (String) JOptionPane.showInputDialog(null, "请输入想猜的数:", "猜数字游戏", JOptionPane.PLAIN_MESSAGE, null, null, "");
        int number = Integer.parseInt(number2);
        while (number != secret) {
            if (number > secret) {
                JOptionPane.showMessageDialog(null, "你猜的数大了,请继续猜", "猜数字游戏", JOptionPane.PLAIN_MESSAGE);
                number2 = (String) JOptionPane.showInputDialog(null, "请输入想猜的数:", "猜数字游戏", JOptionPane.PLAIN_MESSAGE, null, null, "");
                number = Integer.parseInt(number2);
            } else {
                JOptionPane.showMessageDialog(null, "你猜的数小了,请继续猜", "猜数字游戏", JOptionPane.PLAIN_MESSAGE);
                number2 = (String) JOptionPane.showInputDialog(null, "请输入想猜的数:", "猜数字游戏", JOptionPane.PLAIN_MESSAGE, null, null, "");
                number = Integer.parseInt(number2);
            }
        }
        JOptionPane.showMessageDialog(null, "恭喜你,你猜对了,电脑生成的随机数是" + secret, "猜数字游戏", JOptionPane.PLAIN_MESSAGE);
    }
}
作者原创,转载请写明出处
好的,以下是一个简单的猜数字游戏GUIJava代码示例: ```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; public class GuessNumberGame extends JFrame implements ActionListener { private int randNum; private int numGuesses; private final JTextField guessField; private final JTextArea resultArea; public GuessNumberGame() { setTitle("Guess Number Game"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); JPanel topPanel = new JPanel(); topPanel.setLayout(new FlowLayout()); JLabel guessLabel = new JLabel("Guess a number between 1 and 100: "); topPanel.add(guessLabel); guessField = new JTextField(10); topPanel.add(guessField); JButton guessButton = new JButton("Guess"); guessButton.addActionListener(this); topPanel.add(guessButton); add(topPanel, BorderLayout.NORTH); JPanel bottomPanel = new JPanel(); bottomPanel.setLayout(new BorderLayout()); JLabel resultLabel = new JLabel("Results:"); bottomPanel.add(resultLabel, BorderLayout.NORTH); resultArea = new JTextArea(); resultArea.setEditable(false); bottomPanel.add(resultArea, BorderLayout.CENTER); add(bottomPanel, BorderLayout.CENTER); setSize(400, 300); setVisible(true); startGame(); } private void startGame() { Random rand = new Random(); randNum = rand.nextInt(100) + 1; numGuesses = 0; resultArea.setText(""); } private void handleGuess(int guess) { numGuesses++; if (guess == randNum) { resultArea.append("Congratulations! You guessed the number in " + numGuesses + " guesses.\n"); startGame(); } else if (guess < randNum) { resultArea.append("Too low! Try again.\n"); } else { resultArea.append("Too high! Try again.\n"); } guessField.setText(""); } public void actionPerformed(ActionEvent e) { try { int guess = Integer.parseInt(guessField.getText()); if (guess >= 1 && guess <= 100) { handleGuess(guess); } else { JOptionPane.showMessageDialog(this, "Please enter a number between 1 and 100!"); } } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this, "Please enter a valid number!"); } } public static void main(String[] args) { new GuessNumberGame(); } } ``` 这个程序使用了Java的Swing库来创建GUI界面,通过添加监听器来处理用户输入并进行游戏逻辑。游戏逻辑包括生成一个1到100之间的随机数字,并与用户输入的数字进行比较,直到用户猜对为止。用户每次猜测后,程序会显示一条消息告诉他们猜的数字是太高还是太低,并清空输入框以便下一轮猜测。如果用户猜对了,程序会显示一条消息告诉用户他们猜对了,并重新开始游戏。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值