猜数字小游戏需求
1、 操作界面
1、 开始游戏
2、 查看英雄榜
3、 游戏说明
4、 退出游戏
2、 开始游戏
系统随机生成一个1至100的整数,玩家输入一个数字,系统判断该数字如果等于生成的数字,
则显示,玩家猜中了,并记录玩家姓名和成绩,成绩规则为:第一次猜中100分,第二次猜中80
分,第三次猜中60分,第四次猜中40分,第五次猜中20分。如果该数字不等于生成的数字,则
系统提示没有猜中,并提示是大了还是小了,玩家可连续猜5次,如果都没有猜中,系统提示
“太遗憾了”,不做记录,游戏结束。
3、 查看英雄榜
可以查看玩家姓名和成绩
4、 游戏说明
显示游戏说明:玩家选择1,游戏开始,随机生成一个1~100的整数,玩家输入猜的数,游戏判
断是否正确,如不正确给出提示,如果正确,则记录得分。玩家选择2,可查看游戏成绩。
5、 退出游戏
游戏结束
1.界面实现
2.核心逻辑
3.其他功能
嗯,这个超级简单,不多说了,主要看一下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+"\r\n");
						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("退出游戏!");
	}
}
原文:https://www.cnblogs.com/JeasonIsCoding/p/13232545.html