php网站开发技术优点,企业站seo价格,江西seo公司,wordpress去广告需求目标
这个游戏窗口要求玩家猜一个1到100之间的数字。玩家可以在文本框中输入自己的猜测#xff0c;并点击提交按钮进行验证。游戏会给出相应的提示#xff0c;直到玩家猜中正确的数字为止。
效果 源码
/*** author lwh* date 2023/11/28* description 猜数字游戏**/
i…需求目标
这个游戏窗口要求玩家猜一个1到100之间的数字。玩家可以在文本框中输入自己的猜测并点击提交按钮进行验证。游戏会给出相应的提示直到玩家猜中正确的数字为止。
效果 源码
/*** author lwh* date 2023/11/28* description 猜数字游戏**/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class GuessNumberGame extends JFrame {private int randomNumber;private JTextField inputField;private JLabel resultLabel;public GuessNumberGame() {setTitle(猜数字游戏);setSize(300, 200);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setLayout(new FlowLayout());// 生成一个1到100的随机数randomNumber (int) (Math.random() * 100) 1;JLabel promptLabel new JLabel(猜一个1到100之间的数字:);inputField new JTextField(10);JButton submitButton new JButton(提交);resultLabel new JLabel();submitButton.addActionListener(new ActionListener() {Overridepublic void actionPerformed(ActionEvent e) {try {int guess Integer.parseInt(inputField.getText());if (guess randomNumber) {resultLabel.setText(恭喜你猜对了);inputField.setEditable(false);submitButton.setEnabled(false);} else if (guess randomNumber) {resultLabel.setText(猜的数字太小了请再试一次。);} else {resultLabel.setText(猜的数字太大了请再试一次。);}} catch (NumberFormatException ex) {resultLabel.setText(请输入一个有效的数字。);}}});add(promptLabel);add(inputField);add(submitButton);add(resultLabel);setVisible(true);}public static void main(String[] args) {SwingUtilities.invokeLater(new Runnable() {Overridepublic void run() {new GuessNumberGame();}});}
}