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
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
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
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();
}
}
}