猜数字游戏完整版,带注册功能,付费功能 – 恵玩科技

package Game.com.lxgyx;

import Game.com.lxgyx.dao.UserDao;
import Game.com.lxgyx.dao.impl.UserDaoImpl;
import Game.com.lxgyx.pojo.User;
import IO.com.lxgyx_08.GuessNumber;

import java.io.*;
import java.util.Properties;
import java.util.Scanner;

/*
*
* 分析:
* 1 需要存储用户信息
* 2 一个注册和登录类
* 3 需要游戏信息类
* */
public class GueseNumberGame {
    public static void main(String[] args) throws IOException {
        // 把数据加载到集合中
        Properties prop = new Properties();
        Reader r = new FileReader("test\\count.txt");
        prop.load(r);
        r.close();

        // 我自己的程序,我当然知道里面的键是谁
        String value = prop.getProperty("count");
        int number = Integer.parseInt(value);

        if (number > 5) {
            System.out.println("游戏试玩已结束,请付费。");
            System.exit(0);
        } else {
            number++;
            prop.setProperty("count", String.valueOf(number));
            Writer w = new FileWriter("test\\count.txt");
            prop.store(w, null);
            w.close();

            //创建用户功能实例
            UserDao ud=new UserDaoImpl();
            while (true) {
                //首页
                System.out.println("-----------------欢迎光临-----------------");
                System.out.println("1 登录");
                System.out.println("1 注册");
                System.out.println("1 退出");
                //键盘录入
                Scanner sc=new Scanner(System.in);
                String userChoice=sc.nextLine();
                switch (userChoice) {
                    case "1":
                        //登录
                        System.out.println("--------------登录界面--------------");
                        System.out.println("请输入用户名:");
                        String username = sc.nextLine();
                        System.out.println("请输入密码:");
                        String password = sc.nextLine();

                        boolean flag = ud.isLogin(username, password);
                        if (flag) {
                            System.out.println("登录成功,可以开始玩游戏了");

                            System.out.println("你玩吗?y/n");
                            while (true) {
                                String resultString = sc.nextLine();
                                if (resultString.equalsIgnoreCase("y")) {
                                    // 玩游戏
                                    GuessNumber.start();
                                    System.out.println("你还玩吗?y/n");
                                } else {
                                    break;
                                }
                            }
                            System.out.println("谢谢使用,欢迎下次再来");
                            System.exit(0);
                        } else {
                            System.out.println("用户名或者密码有误,登录失败");
                        }
                        break;
                    case "2":
                        //注册
                        System.out.println("--------------注册界面--------------");
                        System.out.println("请输入用户名:");
                        String newUsername = sc.nextLine();
                        System.out.println("请输入密码:");
                        String newPassword = sc.nextLine();

                        // 把用户名和密码封装到一个对象中

                        User user = new User();
                        user.setUsername(newUsername);
                        user.setUserpassword(newPassword);

                        ud.regist(user);
                        System.out.println("注册成功");
                        break;
                    case "3":
                        System.out.println("谢谢使用,欢迎下次再来");
                        System.exit(0);
                        //退出
                        break;
                    default:
                        //提示
                        System.out.println("请输入 1 或者 2  或者  3");
                        break;

                }
            }
        }


    }
}
package Game.com.lxgyx.pojo;

public class User {
    public User() {
    }
    private String username;

    public User(String username, String userpassword) {
        this.username = username;
        this.userpassword = userpassword;
    }

    private String userpassword;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserpassword() {
        return userpassword;
    }

    public void setUserpassword(String userpassword) {
        this.userpassword = userpassword;
    }
}
package Game.com.lxgyx.Game;

import java.util.Scanner;

public class GuseNumber {
    public GuseNumber() {
    }
    //猜数字小游戏


    public static void start(){
        //随机数
        int number=(int) (Math.random()*100)+1;
        //用户输入
        int count=0;//统计次数
        while (true){
            Scanner sc=new Scanner(System.in);
            System.out.println("请输入您要猜的数组(1-100):");
            int userGuess=sc.nextInt();

            if (userGuess>1&&userGuess<100){
                if (userGuess>number){
                    System.out.println("您猜的数是:"+userGuess+",大了.");
                }else if (userGuess<number){
                    System.out.println("您猜的数是:"+userGuess+",小了");
                }else{
                    System.out.println("您猜的数是:"+userGuess+",恭喜您,猜中了.共猜了"+count+"次");
                }
            }else{
                System.out.println("请输入1-100之间的整数!");
            }
            count++;
        }
    }


}
package Game.com.lxgyx.dao;

import Game.com.lxgyx.pojo.User;

public interface UserDao {
    //登录
    public abstract boolean isLogin(String username,String userpassword);
    //注册
    public abstract void regist(User user);
}
package Game.com.lxgyx.dao.impl;

import Game.com.lxgyx.dao.UserDao;
import Game.com.lxgyx.pojo.User;

import java.io.*;

public class UserDaoImpl implements UserDao {
    //创建文件,存储用户信息
    private static File file=new File("user.txt");
    static {
        try {
            file.createNewFile();
        } catch (IOException e) {
            System.out.println("创建用户信息文件失败");
        }
    }
    @Override
    public boolean isLogin(String username, String userpassword) {
        boolean flag=false;
        BufferedReader br=null;

        try {
            br=new BufferedReader(new FileReader(file));
            String line=null;
            while ((line=br.readLine())!=null){
                //用户名=密码
                String[] datas=line.split("=");
                if (datas[0].equals(username)&&datas[1].equals(userpassword)){
                    flag=true;
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("找不到用户登录信息文件");
        } catch (IOException e) {
            System.out.println("用户登录失败");
        }finally {
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("用户登录释放资源失败");
                }
            }
        }

        return flag;
    }

    @Override
    public void regist(User user) {
        BufferedWriter bw=null;

        try {
            bw=new BufferedWriter(new FileWriter(file,true));
            bw.write(user.getUsername()+"="+user.getUserpassword());
            bw.newLine();
            bw.flush();
        } catch (IOException e) {
            System.out.println("用户注册失败");
        }finally {
            if (bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    System.out.println("用户注册释放资源失败");
                }
            }
        }
    }
}

 

打赏作者