当前位置:   article > 正文

Java-猜数游戏_用java设计一个猜数程序

用java设计一个猜数程序

1.团队名称 团队成员 任务分配

成员负责模块
苑先淼(组长)编码规范窗口编写游戏主体编写生成GUI界面

2项目简介
计算机产生随机数,猜中即胜,猜不中,提示是大了还是小了,继续猜,直至猜到,给出所用时间和评语。保留用户测试成绩,做出成绩排行榜。排行榜存放到文件中。
项目功能架构图
程序流程图
在这里插入图片描述

UML类图
在这里插入图片描述

git提交截图和地址
git地址
在这里插入图片描述

运行截图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

主要代码
窗口编写

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
public class Guider {
    public static void main(String[] args) {
        JFrame frame = new JFrame("猜数字游戏");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
 
        JPanel panel = new JPanel();
        panel.setLayout(null);
        frame.add(panel);
 
        JButton startBtn = new JButton("A 开始游戏");
        startBtn.setBounds(120, 100, 100, 30);
        panel.add(startBtn);
 
        JButton exitBtn = new JButton("B 开始游戏");
        exitBtn.setBounds(280, 100, 100, 30);
        panel.add(exitBtn);
 
        startBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new Main("A");
            }
        });
 
        exitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new Main("B");
            }
        });
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

游戏主体

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;

public class Main {
    int count = 1;
    // 计时
    Date startTime ;
   music bgm = new music();


    Main(String name) {
        bgm.playMusic();
        JFrame frame = new JFrame("猜数字游戏");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        Container con = frame.getContentPane();
        con.setBackground(Color.yellow);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        frame.add(panel);

        JLabel label = new JLabel("请输入一个1-100的数字");
        label.setBounds(180, 20, 200, 30);
        panel.add(label);

        JTextField textField = new JTextField();
        textField.setBounds(180, 60, 100, 30);
        panel.add(textField);

        JButton submitBtn = new JButton("提交");
        submitBtn.setBounds(180, 100, 100, 30);
        panel.add(submitBtn);

        JButton exitBtn = new JButton("退出游戏");
        exitBtn.setBounds(180, 140, 100, 30);
        panel.add(exitBtn);

        JLabel resultLabel = new JLabel();
        resultLabel.setBounds(180, 180, 200, 30);
        panel.add(resultLabel);

        int random = (int) (Math.random() * 100 + 1);
        System.out.println(random);


        submitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 开始计时
                if (count == 1) {
                    startTime = new Date();
                }

                String text = textField.getText();
                int num = Integer.parseInt(text);
                if (num > random) {
                    resultLabel.setText("第" + count + "次回答,猜大了");
                } else if (num < random) {
                    resultLabel.setText("第" + count + "次回答,猜小了");
                } else {
                    resultLabel.setText("第" + count + "次回答,猜对了");
                    // 结束计时
                    Date endTime = new Date();
                    long time = endTime.getTime() - startTime.getTime();
                    JOptionPane.showMessageDialog(null, "恭喜你,猜对了!用时" + time / 1000 + "秒");
                    pushRankingList(name, time / 1000, count);
                }
                count++;
            }
        });

        exitBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new Guider().main(null);
            }
        });
    }


    // 生成排行榜文件
    public void pushRankingList(String name, long time, int count) {
        // 创建文件
        File file = new File("排行榜.txt");
        if (!file.exists()) {
            try {
                file.createNewFile();
                FileWriter fw = new FileWriter(file, true);
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write("排名-姓名-用时(秒)-次数\n");
                bw.close();
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            // 获取行数
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            int line = 0;
            while (br.readLine() != null) {
                line++;
            }
            br.close();
            fr.close();

            FileWriter fw = new FileWriter(file, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(line + "-" + name + "-" + time + "-" + count + "次\n");
            bw.close();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        sortRankinList();
    }

    // 给排行榜排序
    public void sortRankinList() {
        // 读取文件
        ArrayList<String> list = new ArrayList<>();
        File file = new File("排行榜.txt");
        int lineNumber = 1;
        try {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String line = br.readLine();
            while (line != null) {
                if (lineNumber > 1 && !line.isEmpty()) {
                    list.add(line);
                }
                line = br.readLine();
                lineNumber++;
            }
            br.close();
            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 给排行榜排序
        list.sort((o1, o2) -> {
            String[] split1 = o1.split("-");
            String[] split2 = o2.split("-");
            return Integer.parseInt(split1[2]) - Integer.parseInt(split2[2]);
        });

        // 重新写入文件
        try {
            FileWriter fw = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("排名-姓名-用时(秒)-次数\n");
            for (int i = 0; i < list.size(); i++) {
                String name = list.get(i).split("-")[1];
                String time = list.get(i).split("-")[2];
                String count = list.get(i).split("-")[3];
                bw.write((i + 1) + "-" + name + "-" + time + "-" + count + "\n");
            }
            bw.close();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177

背景音乐播放

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import java.io.File;

public class music {
    static Clip clip;
    public static void playMusic() {
        try
        {
            File musicPath = new File("D:\\CloudMusic\\music.wav");

            if(musicPath.exists())
            {
                AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
                clip = AudioSystem.getClip();
                clip.open(audioInput);
                FloatControl gainControl = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
                gainControl.setValue(-20.0f);
                clip.start();
                clip.loop(Clip.LOOP_CONTINUOUSLY);
            }
            else
            {

            }


        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

6课程设计感想
还有很多难的内容实现不了,在实际设计中才发现,书本上理论性的东西与在实际运用中的还是有一定的出入的,所以有些问题不但要深入地理解,而且要不断地更正以前的错误思维。一切问题必须要靠自己一点一滴的解决,而在解决的过程当中你会发现自己在飞速的提升。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/233957
推荐阅读
相关标签
  

闽ICP备14008679号