- <div class="blockcode"><blockquote>package app;
- import java.awt.Frame;
- import javax.swing.JFrame;
- import ui.CSZServiceFrame;
- public class MainApp {
- public static void main(String[] args) {
- CSZServiceFrame csz=new CSZServiceFrame();
- csz.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- csz.setVisible(true);
- }
- }
复制代码- package service;
- import java.util.ArrayList;
- import java.util.Random;
- public class CSZService {
- int index = 0;
- String random = "";
- String[] results = new String[8];
- public boolean equalsRandom(String input) {
- return random.equals(input);
- }
- public void init() {
- for (int i = 0; i < results.length; i++) {
- results[i] = "";
- }
- index = 0;
- random = "";
- ArrayList<String> al = new ArrayList<String>();
- for (int i = 0; i < 10; i++) {
- al.add(i + "");
- }
- Random r = new Random();
- for (int i = 0; i < 4; i++) {
- String num = (String) al.remove(r.nextInt(10 - i));
- random += num;
- }
- //System.out.println("游戏产生的随机数为:" + random);
- }
- public String[] getResult(){
- return results;
- }
- public int getIndex(){
- return index;
- }
- public String getRandom(){
- return random;
- }
- public void creatRsult(String input) {
- int numberRightCount = 0;
- int numberErrorCount = 0;
- for (int i = 0; i < input.length(); i++) {
- char charAt = input.charAt(i);
- if (random.contains(charAt + "")) {
- int n = random.indexOf(charAt);
- if (n == i) {
- numberRightCount++;
- } else {
- numberErrorCount++;
- }
- }
- }
- String result=numberRightCount+"A"+numberErrorCount+"b";
- results[index++]=index+">"+input+"-"+result;
- }
-
- }
复制代码- package ui;
- import java.awt.BorderLayout;
- import java.awt.Font;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- public abstract class CSZFrame extends JFrame {
- protected JTextField[] fields = new JTextField[4];// 创建长度为4的文本框数组
- protected JButton btn = new JButton("确定");// 创建确定按钮
- protected JLabel[] lables = new JLabel[2];// 创建长度为2标签数组
- public CSZFrame() {
- // 初始化界面
- init();
- // 添加组件
- addComponent();
- // 添加监听器
- addListener();
- }
- private void addListener() {
- btn.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- String data=getInputAndValidate();
- //lables[0].setText(data);
- run(data);
- }
- catch (NumberFormatException e1) {
- JOptionPane.showMessageDialog(CSZFrame.this, "请输入一个数字");
- }
- catch(RuntimeException e2){
- JOptionPane.showMessageDialog(CSZFrame.this, "数字重复输入");
-
- }
-
- }
-
- });
- }
- public abstract void run(String input);
- public void clearTextFiels(){
- for (int i = 0; i < fields.length; i++) {
- fields[i].setText("");
- }
- }
- public String getInputAndValidate() throws RuntimeException,NumberFormatException {
- String result = "";
- for (int i = 0; i < fields.length; i++) {
- String input = fields[i].getText();
- int inputNum = Integer.parseInt(input);
- if (result.contains(inputNum + "")) {
- throw new RuntimeException("数组输入重复");
- }
- result += inputNum;
- }
- return result;
- }
- private void addComponent() {
- // 设置布局(3个面板)
- JPanel j1 = new JPanel();
- JPanel j2 = new JPanel();
- JPanel j3 = new JPanel();
- // 设置面板拜访的位置
- this.add(j1, BorderLayout.NORTH);
- this.add(j2);
- this.add(j3, BorderLayout.SOUTH);
- // 创建4个文本框
- for (int i = 0; i < fields.length; i++) {
- // 创建每个文本输入框
- fields[i] = new JTextField(3);// 创建文本输入框,并设置宽度为3
- fields[i].setFont(new Font("宋体", 1, 18));// 设置文本框的字体
- j1.add(fields[i]);// 把文本框添加到面板1中
- }
- j1.add(btn);// 把按钮添加到面板1中
- for (int i = 0; i < lables.length; i++) {
- lables[i] = new JLabel();
- lables[i].setFont(new Font("宋体", 1, 14));// 设置字体
- }
- // 标签1添加到面板2中
- j2.add(lables[0]);
- // 标签2添加到面板3中
- j3.add(lables[1]);
- // lables[1].setText("A:代表数字出现,位置正确 B:代表数字出现,位置错误");
- lables[1]
- .setText("<html><body>A:代表数字出现,位置正确 <br/>B:代表数字出现,位置错误</body></html>");
- }
- private void init() {
- // 设置窗体的宽高,位置,标题,不可改变大小
- this.setSize(300, 300);
- this.setLocation(500, 200);
- this.setTitle("猜数字小游戏");
- this.setResizable(false);
- }
- }
复制代码- package ui;
- import javax.swing.JOptionPane;
- import service.CSZService;
- public class CSZServiceFrame extends CSZFrame {
- private CSZService csz = new CSZService();
- boolean youXiZhuangTai=true;
- public CSZServiceFrame() {
- super();
- csz.init();
- }
-
- public void run(String input) {
- //System.out.println("我被点击了");
- if(youXiZhuangTai==false){
- youXiZhuangTai=true;
- csz.init();
- }
- if(csz.equalsRandom(input)){
- JOptionPane.showMessageDialog(this, "恭喜你猜对了");
- youXiZhuangTai=false;
- clearTextFiels();
- return;
- }
- csz.creatRsult(input);
- String[] results= csz.getResult();
- String date="<html><body>";
- for (int i = 0; i < results.length; i++) {
- date+=results[i]+"<br/>";
- }
-
- date+="</body></html>";
- lables[0].setText(date);
- if(csz.getIndex()==8){
- JOptionPane.showMessageDialog(this, "已猜8次,您的智商捉急,游戏结束,正确的数字为:"+csz.getRandom());
- youXiZhuangTai=false;
- clearTextFiels();
-
- }
- }
- }
复制代码
|
|