猜数字小游戏

一、项目介绍

猜数字小游戏,先获得一个随机数再进行猜测,若猜测的数不是该随机数,则提示偏大或偏小,若是则显示猜测的次数和用时,并可以统计每次参与游戏的所有人的排名,排名按猜测次数从小到大降序,并将排名写入文件。

二、 功能图

在这里插入图片描述

三、功能代码:

1.随机数的生成

int number = (int) (Math.random() * 100);

2.猜测数与随机数是否相等/猜测数与随机数的大小比较/猜测次数的统计

do {
				guess = sc.nextInt();
				if (number < guess) {
					System.out.println("猜大了噢,继续猜");
					count++;
					score[j] += 1;
				} else if (number > guess) {
					System.out.println("猜小了噢,继续猜");
					count++;
					score[j] += 1;
				} else {
					count++;
					score[j] += 1;
					break;
				}
			} while (true);
var foo = 'bar';

3.猜测用时的记录

long StarTtime = System.currentTimeMillis();
long EndTime = System.currentTimeMillis();
System.out.println("一共用时" + (EndTime - StarTtime) / 1000 + "秒");

4.按猜测次数从小到大排名

List<Map.Entry<String, String>> lstEntry = new ArrayList<>(hashmap.entrySet());
		Collections.sort(lstEntry, ((o1, o2) -> {
			return o1.getValue().compareTo(o2.getValue());
		}));

5.将游戏排名输入文件for (int i = 1; i <= lstEntry.size(); i++) {

			System.out
					.println("第" + i + "名" + " " + lstEntry.get(i - 1).getKey() + ":" + lstEntry.get(i - 1).getValue());
			try {
				wr = new FileWriter("D:/猜数游戏记录/猜数游戏.txt", true);
				wr.write("\r\n");
				wr.write("时间:" + date);
				wr.write("\r\n");
				wr.write("玩家:" + lstEntry.get(i - 1).getKey());
				wr.write("\r\n");
				wr.write("猜数次数:" + lstEntry.get(i - 1).getValue());
				wr.write("\r\n");
				wr.write("\r\n");
				wr.write("*************************************");
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					wr.close();
				} catch (IOException e) {
					e.printStackTrace();

		}));

四、运行截图

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

五、完整代码

1.写入文件版

package homework;

import java.io.*;
import java.util.*;

public class Guessnum {

	public static void main(String args[]) throws IOException {

		Map<String, String> hashmap = new HashMap<>();
		System.out.println("一起来猜数字吧!");
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入参与的人数:");
		int PersonNumber = sc.nextInt();
		Writer wr = null;
		int guess;
		int[] score = new int[PersonNumber];
		String[] names = new String[PersonNumber];
		Date date = new Date();

		for (int j = 0; j < PersonNumber; j++) {
			int number = (int) (Math.random() * 100);
			int count = 0;
			System.out.println("****************************");
			System.out.println("请输入姓名:");
			String Personname = sc.next();
			names[j] = Personname;
			System.out.println("开始!");
			System.out.println("请输入大于等于0且小于100的数字");
			long StarTtime = System.currentTimeMillis();

			do {
				guess = sc.nextInt();
				if (number < guess) {
					System.out.println("猜大了噢,继续猜");
					count++;
					score[j] += 1;
				} else if (number > guess) {
					System.out.println("猜小了噢,继续猜");
					count++;
					score[j] += 1;
				} else {
					count++;
					score[j] += 1;
					break;
				}
			} while (true);
			String count2 = Integer.toString(count);
			long EndTime = System.currentTimeMillis();
			System.out.println("这个数字是" + number);
			System.out.println("第" + (j + 1) + "位玩家" + Personname + "共猜了" + count + "次");
			System.out.println("一共用时" + (EndTime - StarTtime) / 1000 + "秒");
			System.out.println("****************************");
			hashmap.put(Personname, count2);

			if (count == 1) {
				System.out.println("运气爆棚了!");
			} else if (count >= 2 && count <= 10) {
				System.out.println("运气还可以噢!");
			} else {
				System.out.println("运气不是很好噢!");
			}

		}

		List<Map.Entry<String, String>> lstEntry = new ArrayList<>(hashmap.entrySet());
		Collections.sort(lstEntry, ((o1, o2) -> {
			return o1.getValue().compareTo(o2.getValue());
		}));

		System.out.println("*************************************");
		System.out.println("********排行榜********");
		System.out.println("本次得分排名如下");

		for (int i = 1; i <= lstEntry.size(); i++) {
			System.out
					.println("第" + i + "名" + " " + lstEntry.get(i - 1).getKey() + ":" + lstEntry.get(i - 1).getValue());
			try {
				wr = new FileWriter("D:/猜数游戏记录/猜数游戏.txt", true);
				wr.write("\r\n");
				wr.write("时间:" + date);
				wr.write("\r\n");
				wr.write("玩家:" + lstEntry.get(i - 1).getKey());
				wr.write("\r\n");
				wr.write("猜数次数:" + lstEntry.get(i - 1).getValue());
				wr.write("\r\n");
				wr.write("\r\n");
				wr.write("*************************************");
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					wr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
	}
}

2.GUI版

package homework;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class MyFrame extends JFrame {
int number;
JLabel label;
JTextField text;
JButton button1,button2;
PoliceListener policeListener;
JLabel tip;
static int count=0;

	public static void main(String args[]) {
		MyFrame myFrame = new MyFrame();
	}

	public MyFrame() {
		init();
		setTitle("猜数字");
		setBounds(300,300,260,150);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		validate();
		
	}
	void init() {
		setLayout(new FlowLayout());
		
		
		
		button1 = new JButton("得到一个随机数");
		add(button1);
		
		tip=new JLabel("你已经猜了"+count+"次",JLabel.LEFT);
		add(tip);
		
		label = new JLabel("请输入你的猜测:",JLabel.CENTER);
		label.setBackground(Color.cyan);
		add(label);
		
		text = new JTextField("0",10);
		add(text);
		
		button2 = new JButton("确定");
		add(button2,"South");
		
		policeListener = new PoliceListener();
		button1.addActionListener(policeListener);
		button2.addActionListener(policeListener);
	}
	
	class PoliceListener implements ActionListener{
		public void actionPerformed (ActionEvent e) {
			if(e.getSource()==button1) {
				number = (int)(Math.random()*100)+1;
				label.setText("请输入你的猜测");
				text.setText(null);
			}
			else if(e.getSource()==button2) {
				int guess = 0;
				try {
					guess = Integer.parseInt(text.getText());
					if(guess == number) {
						label.setText("猜对了!");
					}else if(guess < number) {
						tip.setText("你已经猜了"+(++count)+"次");
						label.setText("猜小了!");
						text.setText(null);
					}else {
						tip.setText("你已经猜了"+(++count)+"次");
						label.setText("猜大了!");
						text.setText(null);
					}
				}catch(NumberFormatException event) {
					label.setText("请输入数字字符");
				}
			}
		}
	}
		}

六、参考链接

链接: link.https://www.bilibili.com/video/BV1iC4y1a73G?spm_id_from=333.1007.top_right_bar_window_history.content.click

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值