猜数小游戏升级版(IO流实现,对IO流进行加强理解运用)

猜数小游戏(IO流实现,对IO流进行加强理解运用)

文章目录

    • 猜数小游戏(IO流实现,对IO流进行加强理解运用)
  • 前言
  • 一、设计简单的猜数游戏
    • 1.游戏内容
  • 二、猜数游戏逻辑及实现
    • 1.需要实现的逻辑
    • 2.代码实现
    • 3.需要注意的问题
  • 三、猜拳小游戏
    • 1.代码实现
    • 2.代码解读
  • 四、修改密码
  • 总结


前言

猜数小游戏的设计比较简单,下图是设计的逻辑代码块,其中的info是用户的信息,里面的信息主要有密码,账户余额,以及次数,用IO流的相关知识完成实现的。其中CMUtility以及TSUtility是自己写的工具包,可以不用管。
猜数小游戏升级版(IO流实现,对IO流进行加强理解运用)_第1张图片

一、设计简单的猜数游戏

1.游戏内容

随机生成数字,让我们去猜。如果大的话就提示(你输入的数字大了),如果输入的数字比猜的数字小的话就提示(你输入的数字小了)。如果相等的话就提示(你真他娘的是个天才,猜对了)如此的进行猜测数字,直到猜对为止

package TheGamePackage;
import java.util.Random;
import java.util.Scanner;

import static TheGamePackage.TSUtility.readKeyBoard;

public class Game {
    public static void gamenumber(){
        Scanner sc=new Scanner(System.in);
        Random r=new Random();
        boolean a=true;
        int i = r.nextInt(100) + 1;
        while (a){
            System.out.println("测试数字:"+i);
            System.out.println("请输入你要猜的数字(1-100)");
            int i1 = readInt();
            if (i1>100||i1<1){
                System.out.println("你的输入有误");
            }else if (i1>i){
                System.out.println("你输入的数字大了");
            }else if (i1<i){
                System.out.println("你输入的数字小了");
            }else{
                System.out.println("你真他娘的是个天才,猜的数字为:"+i);
                a=false;
            }
        }
    }

    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(3, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误,请重新输入:");
            }
        }
        return n;
    }
}

二、猜数游戏逻辑及实现

1.需要实现的逻辑

首先是登陆账户,如果没有账户的话就创建账户,然后用账户以及密码去登陆账户,新账户有一次的免费游玩的机会,如果机会用完就会提示是否充值,如果不充值的话就是退出游戏,如果充值的话就会的到免费赠送的次数以及自己充值的金钱,然后继续游玩。账户的信息里面是有金钱数以及密码和次数的变化的。整个过程用IO流实现的。

2.代码实现

main代码

package TheGamePackage;

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

public class GamePlay {
    public static void main(String[] args) throws IOException{
        Menu();
    }
    public static void Menu() throws IOException {
        boolean b=true;
        while (b){
            System.out.println("1.登陆账号----->开始猜数游戏");
            System.out.println("2.修改密码");
            System.out.println("3.猜拳小游戏");
            System.out.println("4.赞助作者");
            System.out.println("5.退出游戏");
            int a= CMUtility.readMenuSelection();
            switch (a){
                case '1':
                    Log();
                    break;
                case '2':
                    change();
                    break;
                case '3':
                    guessmethod();
                    break;
                case '4':
                    new PayView();
                    break;
                case '5':
                    System.out.println("确定退出(Y/y),再考虑一下(N/n)");
                    char c = CMUtility.readConfirmSelection();
                    if (c=='Y'||c=='y'){
                        System.out.println("感谢使用,拜拜");
                        b=false;
                        System.exit(0);
                    }

            }
        }

    }

创建一个guessmethod类

 public static void guessmethod() throws IOException {
        Scanner sc=new Scanner(System.in);
        Random r=new Random();
        //系统自动出的
        ArrayList<String> arrayList=new ArrayList<>();
        arrayList.add("石头");
        arrayList.add("剪刀");
        arrayList.add("布");
        //我出的
        System.out.println("请输入你要出的(石头,剪刀,布)");
        String s = TSUtility.readKeyBoard(2, false);
        if (!s.equals("石头")&&!s.equals("剪刀")&&!s.equals("布")){
            System.out.println("你的输入有误");
            guessmethod();
        }else {
            int i = r.nextInt(3);
            if (arrayList.get(i).equals("石头")&&s.equals("石头")){
                System.out.println("平局");
            }else if (arrayList.get(i).equals("石头")&&s.equals("剪刀")){
                System.out.println("很遗憾,输了");
            }else if (arrayList.get(i).equals("石头")&&s.equals("布")){
                System.out.println("恭喜你,赢了");
            }else if (arrayList.get(i).equals("剪刀")&&s.equals("石头")){
                System.out.println("恭喜你,赢了");
            }else if (arrayList.get(i).equals("剪刀")&&s.equals("剪刀")){
                System.out.println("平局");
            }else if (arrayList.get(i).equals("剪刀")&&s.equals("布")){
                System.out.println("很遗憾,输了");
            }else if (arrayList.get(i).equals("布")&&s.equals("石头")){
                System.out.println("很遗憾,输了");
            }else if (arrayList.get(i).equals("布")&&s.equals("剪刀")){
                System.out.println("恭喜你,赢了");
            }else if (arrayList.get(i).equals("布")&&s.equals("布")){
                System.out.println("平局");
            }
            System.out.println("继续玩?y/n");
            char c = CMUtility.readConfirmSelection();
            if (c=='Y'||c=='y'){
                guessmethod();
            }else {
                Menu();
            }
        }


    }

创建一个Log类

 public static void Log() throws IOException{
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入你的账号(最多六位)");
        String name=TSUtility.readKeyBoard(6,false);
        File f2=new File("E:\\javawork\\dierguan\\src\\TheGamePackage\\Info");
        File[] files = f2.listFiles();
        int ren=0;
        int mianfei=1;
        for (File f:files
        ) {
            if (f.getName().equals(name)){
                ren++;
            }
        }
        if (ren==0){
            System.out.println("没有你的账户,请注册");
            System.out.println("请输入你的密码");
            int i = CMUtility.readInt();
            Properties p1=new Properties();
            p1.setProperty("money","0");
            p1.setProperty("password",String.valueOf(i));
            p1.setProperty("count",String.valueOf(mianfei));
            File f1=new File("E:\\javawork\\dierguan\\src\\TheGamePackage\\Info",name);
            BufferedWriter bu1=new BufferedWriter(new FileWriter(f1));
            p1.store(bu1,null);
    }else {
            File f3=new File("E:\\javawork\\dierguan\\src\\TheGamePackage\\Info",name);
            BufferedReader bu2=new BufferedReader(new FileReader(f3));
            Properties p2=new Properties();
            p2.load(bu2);
            bu2.close();
            System.out.println("请输入你的密码");
            int i1 = CMUtility.readInt();
            if (p2.getProperty("password").equals(String.valueOf(i1))){
                youwan1(name);
            } else {
                System.out.println("密码错误");
                Log();

            }
             }
        }

创建一个youwan1类

public static void youwan1(String name) throws IOException{
            File f3=new File("E:\\javawork\\dierguan\\src\\TheGamePackage\\Info",name);
            BufferedReader bu2=new BufferedReader(new FileReader(f3));
            Properties p2=new Properties();
            p2.load(bu2);
            bu2.close();
            Set<String> strings = p2.stringPropertyNames();
            System.out.println("欢迎"+name);
            System.out.println("你的余额为:"+p2.getProperty("money")+"元");
            System.out.println("你的游玩次数还有"+p2.getProperty("count")+"次");
            String count1= p2.getProperty("count");
            String money = p2.getProperty("money");
            if (Integer.parseInt(count1)>0){
                System.out.println("继续游玩?y/n");
                char c = CMUtility.readConfirmSelection();
                if (c=='Y'||c=='y'){
                    Game.gamenumber();
                    BufferedReader bu3=new BufferedReader(new FileReader(f3));
                    Properties p3=new Properties();
                    p3.load(bu3);
                    int cishu = Integer.parseInt(p3.getProperty("count"));
                    cishu--;
                    Properties p4=new Properties();
                    for (String s1:strings
                    ) {
                        if (s1.equals("count")){
                            p4.setProperty(s1,String.valueOf(cishu));
                        }else {
                            p4.setProperty(s1,p3.getProperty(s1));
                        }
                    }
                    bu3.close();
                    BufferedWriter bu4=new BufferedWriter(new FileWriter(f3));
                    p4.store(bu4,null);
                    bu4.close();
                }else {
                    Menu();
                }
                youwan1(name);
            } else if (Integer.parseInt(count1)<=0&& Integer.parseInt(money)==0){
                System.out.println("你的余额以及次数不足");
                System.out.println("继续游玩?y/n");
                char c = CMUtility.readConfirmSelection();
                if (c=='Y'||c=='y'){
                    chongzhi(name);
                    System.out.println("继续游玩?y/n");
                    char c2 = CMUtility.readConfirmSelection();
                    if (c2=='Y'||c2=='y'){
                        Game.gamenumber();
                        BufferedReader bu3=new BufferedReader(new FileReader(f3));
                        Properties p3=new Properties();
                        p3.load(bu3);
                        int cishu = Integer.parseInt(p3.getProperty("count"));
                        cishu--;
                        Properties p4=new Properties();
                        for (String s1:strings
                        ) {
                            if (s1.equals("count")){
                                p4.setProperty(s1,String.valueOf(cishu));
                            }else {
                                p4.setProperty(s1,p3.getProperty(s1));
                            }
                        }
                        bu3.close();
                        BufferedWriter bu4=new BufferedWriter(new FileWriter(f3));
                        p4.store(bu4,null);
                        bu4.close();
                        youwan1(name);
                    }else {
                        Menu();
                    }
                }else {
                    Menu();
                }
            }else if (Integer.parseInt(count1)<=0&& Integer.parseInt(money)>0) {
                System.out.println("继续玩y/n?");
                char c = CMUtility.readConfirmSelection();
                if (c == 'Y' || c == 'y') {
                    System.out.println("继续玩y/n?注意100一次哟");
                    char c4 = CMUtility.readConfirmSelection();
                    if (c4 == 'Y' || c4 == 'y') {
                        youwan2(name);
                        youwan1(name);
                    } else {
                        Menu();
                    }
                }
            }
        }

创建一个youwan2类

public static void youwan2(String name) throws IOException{
        //当没有次数是消费钱
        File f3=new File("E:\\javawork\\dierguan\\src\\TheGamePackage\\Info",name);
        BufferedReader bu2=new BufferedReader(new FileReader(f3));
        Properties p2=new Properties();
        p2.load(bu2);
        bu2.close();
        Set<String> strings = p2.stringPropertyNames();
        Game.gamenumber();
        BufferedReader bu3=new BufferedReader(new FileReader(f3));
        Properties p3=new Properties();
        p3.load(bu3);
        int cishu = Integer.parseInt(p3.getProperty("money"));
        cishu=cishu-100;
        Properties p4=new Properties();
        for (String s1:strings
        ) {
            if (s1.equals("money")){
                p4.setProperty(s1,String.valueOf(cishu));
            }else {
                p4.setProperty(s1,p3.getProperty(s1));
            }
        }
        bu3.close();
        BufferedWriter bu4=new BufferedWriter(new FileWriter(f3));
        p4.store(bu4,null);
        bu4.close();
    }

创建一个zhongzhi类

public static void chongzhi(String name) throws IOException{
        File f1=new File("E:\\javawork\\dierguan\\src\\TheGamePackage\\Info",name);
            System.out.println("余额不足,是否充值y/n");
            char c1 = CMUtility.readConfirmSelection();
            if (c1=='Y'||c1=='y'){
                System.out.println("充值100送5次");
                System.out.println("充值200送7次");
                System.out.println("充值300送8次");
                System.out.println("充值400送9次");
                System.out.println("充值500送15次");
                System.out.println("请输入你要充值的金额");
                int i = CMUtility.readInt();
                if (i!=100&&i!=200&&i!=300&&i!=400&&i!=500){
                    System.out.println("金额输入错误");
                    chongzhi(name);
                }else if (i==100){
                    BufferedReader bu2=new BufferedReader(new FileReader(f1));
                    Properties p2=new Properties();
                    p2.load(bu2);
                    Set<String> names = p2.stringPropertyNames();
                    Properties p1=new Properties();
                    bu2.close();
                    for (String s:names
                    ) {
                        if (s.equals("count")){
                            p1.setProperty(s,String.valueOf(5));
                        }else{
                            p1.setProperty(s,p2.getProperty(s));
                        }
                    }
                    p1.setProperty("money","100");
                    BufferedWriter bu1=new BufferedWriter(new FileWriter(f1));
                    p1.store(bu1,null);
                    bu1.close();

                }else if (i==200){
                    BufferedReader bu3=new BufferedReader(new FileReader(f1));
                    Properties p3=new Properties();
                    p3.load(bu3);
                    Set<String> names = p3.stringPropertyNames();
                    Properties p4=new Properties();
                    bu3.close();
                    for (String s:names
                    ) {
                        if (s.equals("count")){
                            p4.setProperty(s,String.valueOf(7));
                        }else{
                            p4.setProperty(s,p3.getProperty(s));
                        }
                    }
                    p4.setProperty("money","200");
                    BufferedWriter bu1=new BufferedWriter(new FileWriter(f1));
                    p4.store(bu1,null);
                    bu1.close();
                }else if (i==300){
                    BufferedReader bu2=new BufferedReader(new FileReader(f1));
                    Properties p2=new Properties();
                    p2.load(bu2);
                    Set<String> names = p2.stringPropertyNames();
                    Properties p1=new Properties();
                    bu2.close();
                    for (String s:names
                    ) {
                        if (s.equals("count")){
                            p1.setProperty(s,String.valueOf(8));
                        }else{
                            p1.setProperty(s,p2.getProperty(s));
                        }
                    }
                    p1.setProperty("money","300");
                    BufferedWriter bu1=new BufferedWriter(new FileWriter(f1));
                    p1.store(bu1,null);
                    bu1.close();
                }else if (i==400){
                    BufferedReader bu2=new BufferedReader(new FileReader(f1));
                    Properties p2=new Properties();
                    p2.load(bu2);
                    Set<String> names = p2.stringPropertyNames();
                    Properties p1=new Properties();
                    bu2.close();
                    for (String s:names
                    ) {
                        if (s.equals("count")){
                            p1.setProperty(s,String.valueOf(9));
                        }else{
                            p1.setProperty(s,p2.getProperty(s));
                        }
                    }
                    p1.setProperty("money","400");
                    BufferedWriter bu1=new BufferedWriter(new FileWriter(f1));
                    p1.store(bu1,null);
                    bu1.close();
                }else if (i==500){
                    BufferedReader bu2=new BufferedReader(new FileReader(f1));
                    Properties p2=new Properties();
                    p2.load(bu2);
                    Set<String> names = p2.stringPropertyNames();
                    Properties p1=new Properties();
                    bu2.close();
                    for (String s:names
                    ) {
                        if (s.equals("count")){
                            p1.setProperty(s,String.valueOf(15));
                        }else{
                            p1.setProperty(s,p2.getProperty(s));
                        }
                    }
                    p1.setProperty("money","500");
                    BufferedWriter bu1=new BufferedWriter(new FileWriter(f1));
                    p1.store(bu1,null);
                    bu1.close();
                }
            }else {
                Menu();
            }

    }

以上就是具体的代码的实现

3.需要注意的问题

需要进行条件的判断
1.当次数=0且余额!=0时就要判断是否用余额去玩耍,需要规定多少钱玩一次。
2.当次数=0且余额=0是就要判断是否进行充值,充值的时候就要注意规定的充值金额,其余的金额无法充值。
3.当次数!=0时就正常的玩耍

三、猜拳小游戏

1.代码实现

public static void guessmethod() throws IOException {
        Scanner sc=new Scanner(System.in);
        Random r=new Random();
        //系统自动出的
        ArrayList<String> arrayList=new ArrayList<>();
        arrayList.add("石头");
        arrayList.add("剪刀");
        arrayList.add("布");
        //我出的
        System.out.println("请输入你要出的(石头,剪刀,布)");
        String s = TSUtility.readKeyBoard(2, false);
        if (!s.equals("石头")&&!s.equals("剪刀")&&!s.equals("布")){
            System.out.println("你的输入有误");
            guessmethod();
        }else {
            int i = r.nextInt(3);
            if (arrayList.get(i).equals("石头")&&s.equals("石头")){
                System.out.println("平局");
            }else if (arrayList.get(i).equals("石头")&&s.equals("剪刀")){
                System.out.println("很遗憾,输了");
            }else if (arrayList.get(i).equals("石头")&&s.equals("布")){
                System.out.println("恭喜你,赢了");
            }else if (arrayList.get(i).equals("剪刀")&&s.equals("石头")){
                System.out.println("恭喜你,赢了");
            }else if (arrayList.get(i).equals("剪刀")&&s.equals("剪刀")){
                System.out.println("平局");
            }else if (arrayList.get(i).equals("剪刀")&&s.equals("布")){
                System.out.println("很遗憾,输了");
            }else if (arrayList.get(i).equals("布")&&s.equals("石头")){
                System.out.println("很遗憾,输了");
            }else if (arrayList.get(i).equals("布")&&s.equals("剪刀")){
                System.out.println("恭喜你,赢了");
            }else if (arrayList.get(i).equals("布")&&s.equals("布")){
                System.out.println("平局");
            }
            System.out.println("继续玩?y/n");
            char c = CMUtility.readConfirmSelection();
            if (c=='Y'||c=='y'){
                guessmethod();
            }else {
                Menu();
            }
        }


    }

2.代码解读

其实这个很简单,没有用到IO流的相关的知识,就是把剪刀,石头,布,放在List的集合中,然后随机的取出与自己键盘输入的进行比较

四、修改密码

首先时判断是否有账户,如果有的话就进行密码的修改。如果,没有的话就无法修改

 public static void change()throws IOException{
        System.out.println("请输入你要修改的账号名(最多六位)");
        String name=TSUtility.readKeyBoard(6,false);
        File f1=new File("E:\\javawork\\dierguan\\src\\TheGamePackage\\Info",name);
        //File f2=new File("E:\\javawork\\dierguan\\src\\TheGamePackage\\Info",name);
        if (!f1.exists()){
            System.out.println("账户不存在");
            System.out.println("继续修改y/n?");
            char c = CMUtility.readConfirmSelection();
            if (c == 'Y' || c == 'y') {
                change();
                } else {
                    Menu();
                }
        }else {
            BufferedReader bu1=new BufferedReader(new FileReader(f1));
            Properties p1=new Properties();
            p1.load(bu1);
            System.out.println("请输入你的新密码");
            int i = TSUtility.readInt();
            Set<String> names = p1.stringPropertyNames();
            Properties p2=new Properties();
            for (String s:names
            ) {
                if (s.equals("password")){
                    p2.setProperty(s,String.valueOf(i));
                }else {
                    p2.setProperty(s,p1.getProperty(s));
                }
            }
            BufferedWriter bu2=new BufferedWriter(new FileWriter(f1));
            p2.store(bu2,null);
            bu1.close();
            bu2.close();

        }


    }

总结

IO流的相关知识我已经在上一篇的博客中进行了总结,这里是对IO流的相关的知识进行运用,对IO流的相关知识更加的牢固

你可能感兴趣的:(java)