zoukankan      html  css  js  c++  java
  • JAVA基础-猜数字小游戏

    1.需求描述

    猜数字小游戏需求

    1、 操作界面

    1、 开始游戏

    2、 查看英雄榜

    3、 游戏说明

    4、 退出游戏

    2、 开始游戏

    系统随机生成一个1至100的整数,玩家输入一个数字,系统判断该数字如果等于生成的数字,

    则显示,玩家猜中了,并记录玩家姓名和成绩,成绩规则为:第一次猜中100分,第二次猜中80

    分,第三次猜中60分,第四次猜中40分,第五次猜中20分。如果该数字不等于生成的数字,则

    系统提示没有猜中,并提示是大了还是小了,玩家可连续猜5次,如果都没有猜中,系统提示

    “太遗憾了”,不做记录,游戏结束。

    3、 查看英雄榜

    可以查看玩家姓名和成绩

    4、 游戏说明

    显示游戏说明:玩家选择1,游戏开始,随机生成一个1~100的整数,玩家输入猜的数,游戏判

    断是否正确,如不正确给出提示,如果正确,则记录得分。玩家选择2,可查看游戏成绩。

    5、 退出游戏

    游戏结束

    2.实现思路

    1.界面实现

    • java程序一直运行,点击“退出游戏”后退出程序。
    • 菜单用多路选择实现(switch)

    2.核心逻辑

    • 随机生成一个随机数
    • 与输入的数进行比较,利用if进行三次判断
    • 根据猜的次数计算得分

    3.其他功能

    • 按Y返回上一级菜单,利用while循环,输入Y跳出循环
    • 提示还有几次机会,如果等于0则不显示,利用三元运算符
      • 分数记录和读取,利用javaIO读写文本

    3.界面实现

    4.逻辑实现

    5.其他实现

    6.代码

    嗯,这个超级简单,不多说了,主要看一下IO就可以了

    import java.io.FileWriter;
    import java.io.IOException;
    import java.time.Year;
    import java.util.Scanner;
    
    public class nums {
    
    	//按Y返回上级菜单;
    	public static void waitY() {
    		while (true) {
    			System.out.println("按Y返回上级");
    			Scanner scanner3 = new Scanner(System.in);
    			String string = scanner3.next();
    			if("Y".equals(string))
    				break;
    		}
    	}
    	
    	public static void main(String[] args)  {
    		// TODO Auto-generated method stub
    		boolean flag = true;
    		String filepath = System.getProperty("user.dir")+"\src\score.txt"; 
    		System.out.println(filepath);
    		while(flag) {
    			System.out.println("****猜数字游戏****");
    			System.out.println("*1.开始游戏               *");
    			System.out.println("*2.查看英雄榜           *");
    			System.out.println("*3.游戏说明               *");
    			System.out.println("*0.退出游戏               *");
    			System.out.println("****************");
    			
    			System.out.println("请输入:");
    			Scanner scanner = new Scanner(System.in);
    			int c = scanner.nextInt();
    			
    			switch (c) {
    //			开始游戏
    			case 1:
    				//随机生成1-100的正整数
    				int target = (int) (Math.random() * 10);
    				if(target ==0)   target = 1;
    				int score = 0;
    				//进行猜测
    				for(int i = 0; i < 5;i++) {
    					System.out.println("请输入你猜测的数字:");
    					Scanner scanner2 = new Scanner(System.in);
    					int num = scanner2.nextInt();
    					if( num > target) {
    						System.out.println("你猜大了!");
    						System.out.println( 4-i>0 ?"你还能猜的次数为:"+(4-i) : " ");
    					}else if (num < target) {
    						System.out.println("你猜小了!");
    						System.out.println( 4-i>0 ?"你还能猜的次数为:"+(4-i) : " ");
    					}else {
    						System.out.println("你猜对了!");
    						score = 100 - (i*20);
    						break;
    					}
    				}
    				if(score > 0) {
    					//记录成绩
    					System.out.println("您的得分为:"+score);
    					//记录姓名
    					System.out.println("请输入您的姓名:");
    					Scanner scanner4 = new Scanner(System.in);
    					String name = scanner4.next();
    					//写入文件
    					try {
    						FileWriter fWriter = new FileWriter(filepath,true);
    						fWriter.write(name+","+score+"
    ");
    						fWriter.flush();
    						fWriter.close();
    						System.out.println("记录成功!");
    					} catch (Exception e) {
    						// TODO: handle exception
    						e.printStackTrace();
    					}
    				}
    					
    				else 
    					System.out.println("对不起,您全部没猜对!");
    				waitY();
    				break;
    				
    //			查看英雄榜
    			case 2:
    				System.out.println("英雄榜!");
    				waitY();
    				break;
    //			游戏说明
    			case 3:
    				//按Y返回
    				System.out.println("游戏说明!");
    				waitY();
    				break;
    //			退出游戏
    			case 0:
    				flag = false;
    				break;
    			default:
    				break;
    			}
    		}
    		
    		System.out.println("退出游戏!");
    	}
    }
    
    
  • 相关阅读:
    洛谷 P3178 [HAOI2015]树上操作
    『学习笔记』线段树合并(洛谷P4556)
    『学习笔记』树链剖分(洛谷P3384)
    洛谷 P6218 [USACO06NOV] Round Numbers S
    洛谷 P4999 烦人的数学作业
    洛谷 P4317 花神的数论题
    洛谷P2602 [ZJOI2010]数字计数
    WAMPServer多站点配置
    自定义网站根目录
    网络编程——服务器与多客户端通信
  • 原文地址:https://www.cnblogs.com/JeasonIsCoding/p/13232545.html
Copyright © 2011-2022 走看看