IO流小实验:猜数小游戏程序
一、功能需求
1.首先实现登录注册功能,对其密码格式限制
2.登录成功后进入游戏主菜单页面,有修改密码和赞助作者以及开始游戏等子功能
3.开始猜数游戏进入猜数游戏,其中会有充值和猜数开始;充值会给出相应的充值方案,供用户参考;猜数开始会给出用户剩余猜数游戏次数,根据用户输入的数据判断是否猜数正确;
源码:
package IOliu;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;
public class Game {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) throws InterruptedException, IOException {
//延时加载方法调用
TSUtility.loadSpecialEffects();
//登录注册页面展示
MenuVue.member();
while (true) {
//选择限制调用工具类
char a=TSUtility.readMenuSelection1();
switch (a) {
case '1':
//注册页
Login.login();
break;
case '2':
//登录页
register();
break;
case '3':
System.out.print("您确定要退出吗?请输入(y/n):");
//选择限制
char b=TSUtility.readConfirmSelection();
//加载延时
TSUtility.loadSpecialEffects();
if(b=='Y'||b=='y'){
System.out.println("退出成功!");
System.exit(0);
}else {
System.out.println("您选择了No,未退出系统!\n");
//回车继续操作
TSUtility.readReturn();
//主菜单展示
MenuVue.member();
break;
}
}
}
}
//登录功能
public static void register() throws InterruptedException, IOException {
boolean flag = true;
int in = 5;
while (flag) {
System.out.print("请输入用户名:");
String name = sc.next();
System.out.print("请输入密码:");
String password = sc.next();
TSUtility.loadSpecialEffects();
FileReader fr = new FileReader("E:\\DELL\\后端关卡8-16\\关卡14_文件IO流\\升级版猜拳小游戏\\用户信息.txt");
Properties prop = new Properties();
prop.load(fr);
fr.close();
if (prop.size() == 0) {
System.out.println("您的账号还未注册,请先注册!\n");
//工具类调用,回车继续操作
TSUtility.readReturn();
//登录注册页面展示
MenuVue.member();
flag=false;
} else {
int index=0;
int index1=0;
Set<Object> obj = prop.keySet();
for (Object ob:obj){
if (name.equals(ob)) {
String password1=prop.getProperty((String) ob);
if (password.equals(password1)) {
//登录成功页展示
MenuVue.registerview(name);
//工具类调用,回车继续操作
TSUtility.readReturn();
//进入主菜单页
MainMenu.Mainmenu(name);
//登录注册页面展示
MenuVue.member();
flag=false;
break;
} else {
index1++;
}
} else {
index++;
}
}
if (index!=0){
in--;
if (in>0) {
System.out.println("您输入的账户名不正确!请重新输入账户密码");
System.out.println("您还有"+in+"次机会");
}else {
System.out.println("您已输入5次登录失败!\n");
flag=false;
TSUtility.readReturn();
//退出程序
//System.exit(0);
//登录注册页面展示
MenuVue.member();
}
}
if (index1!=0){
in--;
if (in>0) {
System.out.println("您输入的密码不正确!请重新输入账户密码");
System.out.println("您还有" + in + "次机会");
}else {
System.out.println("您已输入5次登录失败!\n");
flag=false;
TSUtility.readReturn();
//退出程序
//System.exit(0);
//登录注册页面展示
MenuVue.member();
}
}
}
}
}
}
package IOliu;
//注册功能
import java.io.*;
import java.util.Objects;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;
public class Login {
static Scanner sc=new Scanner(System.in);
public static void login() throws InterruptedException, IOException {
File file = new File("E:\\DELL\\后端关卡8-16\\关卡14_文件IO流\\升级版猜拳小游戏\\用户信息.txt");
if (!file.exists()){
file.createNewFile();
}
//FileWriter fw = new FileWriter();
System.out.print("请输入您的用户名:");
//判断用户名是否重复
String name=judge(file);
System.out.print("请输入您的密码(由数字或大小写字母组成且长度大于等于6位):");
//密码限制
String password=judgepassword();
//延时加载
TSUtility.loadSpecialEffects();
if (name.equals("")||password.equals("")){
System.out.println("用户名和密码不能为空,注册失败!\n");
//回车继续
TSUtility.readReturn();
//登录注册页面展示
MenuVue.member();
}else {
FileWriter fw = new FileWriter(file);
Properties prop = new Properties();
prop.setProperty(name,password);
prop.store(fw,null);
fw.close();
System.out.println("注册成功!\n");
//回车继续
TSUtility.readReturn();
//登录注册页面展示
MenuVue.member();
}
}
//判断用户名是否重复
public static String judge(File file) throws IOException {
FileReader fr = new FileReader(file);
Properties prop = new Properties();
prop.load(fr);
fr.close();
String name="";
if(prop.size()==0){
name=TSUtility.readString(15,"");
}else {
boolean flag = true;
while (flag) {
name = TSUtility.readString(15,"");
Set<Object> obj = prop.keySet();
for (Object obj1:obj) {
//String name1=prop.getProperty((String)obj1);
flag = false;
if (name.equals(obj1)) {
System.out.print("用户名已存在,请重新输入:");
flag = true;
break;
}
}
}
}
return name;
}
public static String judgepassword(){
boolean flag = true;
String password="";
while (flag) {
password = TSUtility.readString(15, "");
flag = false;
int in = 0;
if (!Objects.equals(password, "")) {
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z' ||
password.charAt(i) <= 'Z' && password.charAt(i) >= 'A' ||
password.charAt(i) >= '0' && password.charAt(i) <= '9') {
} else {
System.out.println("密码格式有误!由数字或大小写字母组成且长度大于等于6位,请重新输入:");
flag = true;
in++;
break;
}
}
//in==0限制密码不能是中文
if (in == 0 && password.length() >= 6) {
int index = 0, index1 = 0, index2 = 0;
for (int i = 0; i < password.length(); i++) {
if (password.charAt(i) >= 'a' && password.charAt(i) <= 'z') {
index++;
} else if (password.charAt(i) <= 'Z' && password.charAt(i) >= 'A') {
index1++;
} else {
index2++;
}
}
if (index == 0 || index1 == 0 || index2 == 0) {
System.out.println("密码格式有误!由数字或大小写字母组成且长度大于等于6位,请重新输入:");
flag = true;
}
}else {
if (in==0) {
System.out.println("密码格式有误!由数字或大小写字母组成且长度大于等于6位,请重新输入:");
flag = true;
}
}
}
}
return password;
}
}
package IOliu;
//工具类
import java.util.Random;
import java.util.Scanner;
public class TSUtility {
private static Scanner scanner = new Scanner(System.in);
public static char readMenuSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
public static char readMenuSelection1() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2'&&c!='3') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
public static char readMenuSelection2() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
public static char readMenuSelectionPro() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4' && c != '5'&&c!='6') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
public static char readMenuSelectionPro1() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false);
c = str.charAt(0);
if (c != '1' && c != '2' &&
c != '3' && c != '4' && c != '5') {
System.out.print("选择错误,请重新输入:");
} else break;
}
return c;
}
public static int readMenuSelectionPro2() {
int c;
for (; ; ) {
String str = readKeyBoard(3, false);
try {
c = Integer.parseInt(str);
if (c>100||c<0) {
System.out.print("选择错误,请重新输入:");
} else break;
}catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return c;
}
public static void readReturn() {
System.out.print("按回车键继续...");
readKeyBoard(100, true);
}
public static int readInt() {
int n;
for (; ; ) {
String str = readKeyBoard(3, true);
if (str.equals("")) {
int i=0;
return i;
}
try {
n = Integer.parseInt(str);
if (n==100||n==200||n==300||n==400||n==500||n==600||n==700||n==800){
break;
}else {
System.out.print("请按充值提示充值金额!请重新输入:");
}
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
/**
从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public static int readInt(int defaultValue) {
int n;
for (; ; ) {
String str = readKeyBoard(3, true);
if (str.equals("")) {
return defaultValue;
}
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static double readDouble(double defaultValue) {
double n;
for (; ; ) {
String str = readKeyBoard(6, true);
if (str.equals("")) {
return defaultValue;
}
try {
n = Double.parseDouble(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static int readstock() {
int n;
for (; ; ) {
String str = readKeyBoard(6, false);
try {
n = Integer.parseInt(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static Double readDouble() {
Double n;
for (; ; ) {
String str = readKeyBoard(6, false);
try {
n = Double.parseDouble(str);
break;
} catch (NumberFormatException e) {
System.out.print("数字输入错误,请重新输入:");
}
}
return n;
}
public static char readConfirmSelection() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'Y' || c == 'N') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
public static char readConfirmSelection1() {
char c;
for (; ; ) {
String str = readKeyBoard(1, false).toUpperCase();
c = str.charAt(0);
if (c == 'A' || c == 'B'||c=='C') {
break;
} else {
System.out.print("选择错误,请重新输入:");
}
}
return c;
}
/**
从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
*/
public static String readString(int limit, String defaultValue) {
String str = readKeyBoard(limit, true);
return str.equals("")? defaultValue : str;
}
public static String readKeyBoard(int limit, boolean blankReturn) {
String line = "";
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.length() == 0) {
if (blankReturn) return line;
else continue;
}
if (line.length() < 1 || line.length() > limit) {
System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
continue;
}
break;
}
return line;
}
public static void loadSpecialEffects() throws InterruptedException {
System.out.println("请稍等:");
for (int i1 = 1; i1 <= 100; i1++) {
System.out.print("加载中:" + i1 + "%");
Thread.sleep(new Random().nextInt(25) + 1);
if (i1 == 100) {
Thread.sleep(50);
}
System.out.print("\r");
}
}
}
package IOliu;
import java.io.*;
import java.util.Properties;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
public class MainMenu {
public static void Mainmenu(String name) throws InterruptedException, IOException {
boolean flag = true;
//延时加载方法调用
TSUtility.loadSpecialEffects();
//主菜单页功能展示
MenuVue.Mainmenuview();
while (flag) {
//选择限制
char a = TSUtility.readMenuSelectionPro1();
switch (a) {
case '1':
// 开始猜数游戏
game(name);
break;
case '2':
//修改密码
modify(name);
break;
case '3':
//赞助作者(微信支付)
TSUtility.loadSpecialEffects();
new PayView();
//主菜单展示
MenuVue.Mainmenuview();
break;
case '4':
//加载延时
TSUtility.loadSpecialEffects();
System.out.println("退出登录成功!\n");
flag = false;
//回车继续操作
TSUtility.readReturn();
break;
case '5':
System.out.print("您确定要退出吗?请输入(y/n):");
//选择限制
char b = TSUtility.readConfirmSelection();
//加载延时
TSUtility.loadSpecialEffects();
if (b == 'Y' || b == 'y') {
System.out.println("退出成功!");
System.exit(0);
} else {
System.out.println("您选择了No,未退出系统!\n");
//回车继续操作
TSUtility.readReturn();
//主菜单展示
MenuVue.Mainmenuview();
break;
}
}
}
}
private static void game(String name) throws IOException, InterruptedException {
File file = new File("E:\\DELL\\后端关卡8-16\\关卡14_文件IO流\\升级版猜拳小游戏\\游戏次数.txt");
if (!file.exists()){
file.createNewFile();
}
FileReader fr = new FileReader(file);
Properties prop = new Properties();
prop.load(fr);
fr.close();
Set<Object> obj = prop.keySet();
for (Object o:obj){
if (!name.equals(o)){
System.out.println("您还未充值,可以试玩5次!");
FileWriter fw = new FileWriter(file);
prop.setProperty(name,"5");
prop.store(fw,null);
fw.close();
}
}
System.out.println("1.充值\t\t\t2.开始游戏\t\t\t3.返回上一菜单");
System.out.print("请输入您的选择:");
boolean flag=true;
while (flag) {
char c=TSUtility.readMenuSelection1();
switch (c) {
case '1':
//充值
recharge(file,name);
System.out.println("1.充值\t\t\t2.开始游戏\t\t\t3.返回上一菜单");
System.out.print("请输入您的选择:");
break;
case '2':
//开始游戏
demo(file,name);
break;
case '3':
//回车继续操作
TSUtility.readReturn();
//主菜单展示
MenuVue.Mainmenuview();
flag=false;
break;
}
}
}
public static void recharge(File file,String name) throws IOException, InterruptedException {
//加载延时
TSUtility.loadSpecialEffects();
System.out.println("欢迎尊贵的"+name+"玩家");
System.out.println("充值100元,购买10次游戏次数,另送5次游戏次数");
System.out.println("充值200元,购买25次游戏次数,另送5次游戏次数");
System.out.println("充值300元,购买40次游戏次数,另送10次游戏次数");
System.out.println("充值400元,购买55次游戏次数,另送10次游戏次数");
System.out.println("充值500元,购买65次游戏次数,另送15次游戏次数");
System.out.println("充值600元,购买80次游戏次数,另送15次游戏次数");
System.out.println("充值700元,购买90次游戏次数,另送20次游戏次数");
System.out.println("充值800元,购买100次游戏次数,另送30次游戏次数");
System.out.print("请输入您的充值金额:");
int in=TSUtility.readInt();
//加载延时
TSUtility.loadSpecialEffects();
FileReader fr = new FileReader(file);
Properties prop = new Properties();
prop.load(fr);
fr.close();
FileWriter fw = new FileWriter(file);
Set<Object> obj = prop.keySet();
int index=0;
for (Object s:obj) {
if (name.equals(s)) {
index = (Integer.parseInt(prop.getProperty((String) s)));
}
}
if (in==100){
prop.setProperty(name, String.valueOf(index+10+5));
prop.store(fw,null);
fw.close();
System.out.println("充值成功!");
}else if (in==200){
prop.setProperty(name, String.valueOf(index+25+5));
prop.store(fw,null);
fw.close();
System.out.println("充值成功!");
}else if (in==300){
prop.setProperty(name, String.valueOf(index+40+10));
prop.store(fw,null);
fw.close();
System.out.println("充值成功!");
}else if (in==400){
prop.setProperty(name, String.valueOf(index+55+10));
prop.store(fw,null);
fw.close();
System.out.println("充值成功!");
}else if (in==500){
prop.setProperty(name, String.valueOf(index+65+15));
prop.store(fw,null);
fw.close();
System.out.println("充值成功!");
}else if (in==600){
prop.setProperty(name, String.valueOf(index+80+15));
prop.store(fw,null);
fw.close();
System.out.println("充值成功!");
}else if (in==700){
prop.setProperty(name, String.valueOf(index+90+20));
prop.store(fw,null);
fw.close();
System.out.println("充值成功!");
}else if (in==800){
prop.setProperty(name, String.valueOf(index+100+30));
prop.store(fw,null);
fw.close();
System.out.println("充值成功!");
}else {
System.out.println("您未输入金额,充值失败!");
}
}
public static void demo(File file,String name) throws IOException, InterruptedException {
//加载延时
TSUtility.loadSpecialEffects();
FileReader fr = new FileReader(file);
Properties prop = new Properties();
prop.load(fr);
fr.close();
Set<Object> obj = prop.keySet();
int index=0;
for (Object s:obj) {
if (name.equals(s)) {
index = (Integer.parseInt(prop.getProperty((String) s)));
}
}
if (index==0){
System.out.println("您的游戏次数已使用完,不能继续游戏!");
System.out.println("1.充值\t\t\t2.返回上一菜单");
System.out.print("请输入您的选择:");
char c=TSUtility.readMenuSelection2();
switch (c){
case '1':
//充值
recharge(file,name);
break;
case '2':
//加载延时
TSUtility.loadSpecialEffects();
break;
}
}else {
index--;
prop.setProperty(name,String.valueOf(index));
FileWriter fw = new FileWriter(file);
prop.store(fw,null);
fw.close();
System.out.println("您的游戏次数还剩余:"+index+"次");
Random r = new Random();
int number = r.nextInt(101);
//Scanner sc=new Scanner(System.in);
System.out.println("猜数游戏开始:猜数字(0-100)");
System.out.println("随机生成数字:" + number);
while (true) {
System.out.print("请输入你猜测的数据:");
int in = TSUtility.readMenuSelectionPro2();
//加载延时
TSUtility.loadSpecialEffects();
if (in < number) {
System.out.println("您猜的数据小了");
} else if (in > number) {
System.out.println("您猜的数据大了");
} else {
System.out.println("大聪明,恭喜你猜对了!");
//回车继续操作
TSUtility.readReturn();
break;
}
}
}
System.out.println("1.充值\t\t\t2.开始游戏\t\t\t3.返回上一菜单");
System.out.print("请输入您的选择:");
}
private static void modify(String name) throws IOException, InterruptedException {
//加载延时
TSUtility.loadSpecialEffects();
FileReader fr = new FileReader("E:\\DELL\\后端关卡8-16\\关卡14_文件IO流\\升级版猜拳小游戏\\用户信息.txt");
Properties prop = new Properties();
prop.load(fr);
fr.close();
Set<Object> obj = prop.keySet();
for (Object i:obj){
if (i.equals(name)){
String password=prop.getProperty((String)i);
System.out.println("您的原始密码为:"+password);
System.out.print("请输入您的新密码:");
String password1=Login.judgepassword();
//加载延时
TSUtility.loadSpecialEffects();
if (password1.equals("")){
System.out.println("您未输入新密码,修改失败!");
//主菜单展示
MenuVue.Mainmenuview();
}else {
System.out.print("您确认要修改您的密码吗?请输入(y/n):");
char b=TSUtility.readConfirmSelection();
//加载延时
TSUtility.loadSpecialEffects();
if(b=='Y'||b=='y') {
prop.setProperty(name, password1);
FileWriter fw = new FileWriter("E:\\DELL\\后端关卡8-16\\关卡14_文件IO流\\升级版猜拳小游戏\\用户信息.txt");
prop.store(fw,null);
System.out.println("修改成功!\n");
//回车继续操作
TSUtility.readReturn();
//主菜单展示
MenuVue.Mainmenuview();
}else {
System.out.println("您选择了No,您的密码未修改!\n");
//回车继续操作
TSUtility.readReturn();
//主菜单展示
MenuVue.Mainmenuview();
}
}
}
}
}
}
package IOliu;
public class MenuVue {
public static void member(){
System.out.println("*******************************");
System.out.println("* *");
System.out.println("* *");
System.out.println("* 欢迎使用猜数小游戏 *");
System.out.println("* *");
System.out.println("* *");
System.out.println("*******************************");
System.out.println("1.注册账户\t\t\t2.登录账户");
System.out.println("\t\t3.退出系统!");
System.out.print("请输入你的选择:");
}
public static void registerview(String name){
System.out.println("+++++++++++++++++++++++++++++");
System.out.println("+++ +++");
System.out.println("+++ +++");
System.out.println("+++\t\t欢迎 "+name+"\t用户\t+++");
System.out.println("+++ +++");
System.out.println("+++ +++");
System.out.println("+++++++++++++++++++++++++++++");
System.out.println("\t\t\t登录成功!\n");
}
public static void Mainmenuview(){
System.out.println("##############################");
System.out.println("# ~1. 开始猜数游戏~ #");
System.out.println("# ~2. 修改密码 ~ #");
System.out.println("# ~3.赞助作者(微信支付)~ #");
System.out.println("# ~4. 退出登录 ~ #");
System.out.println("# ~5. 退出游戏 ~ #");
System.out.println("##############################");
System.out.print("请输入您的选择:");
}
}
package IOliu;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.File;
public class PayView extends JFrame{
/*
*生成二维码
*下面代码只需要换一个图片即可
*new PayView();//创建这个对象即可展示支付图片
*/
MyPanel mp=null;
public PayView(){
mp=new MyPanel();
this.add(mp);
this.setSize(1273, 809);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args){
new PayView();
}
}
class MyPanel extends JPanel{
Image image=null;
public void paint(Graphics g){
try {
image= ImageIO.read(new File("E:\\DELL\\后端关卡8-16\\关卡14_文件IO流\\升级版猜拳小游戏\\ttt.jpg"));
g.drawImage(image, 0, 0, 1273, 809, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}