猜数小游戏升级版(IO流实现,对IO流进行加强理解运用)
Posted lnwd___
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了猜数小游戏升级版(IO流实现,对IO流进行加强理解运用)相关的知识,希望对你有一定的参考价值。
猜数小游戏(IO流实现,对IO流进行加强理解运用)
文章目录
前言
猜数小游戏的设计比较简单,下图是设计的逻辑代码块,其中的info是用户的信息,里面的信息主要有密码,账户余额,以及次数,用IO流的相关知识完成实现的。其中CMUtility以及TSUtility是自己写的工具包,可以不用管。
一、设计简单的猜数游戏
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 (IntegerJava中的IO流如何理解——精简