话不多说,直接上代码。
一、先来几个工具类下下饭。
1、输入操作限制型的工具类(TSUtility)
package MathDemo; import java.util.Random; import java.util.Scanner; import
java.util.regex.Pattern; 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 readMenuSelectionPro() { 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 void
readReturn() { System.out.print("按回车键继续..."); readKeyBoard(100, true); } public
static int readInt() { int n; for (; ; ) { String str = readKeyBoard(2, false);
try { n = Integer.parseInt(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 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"); } } //把全为数字的字符串转换为int类型,并返回其数值 public static int
printInt(String x){ Scanner sc = new Scanner(System.in);
if(isNumberic(x)==true){ int y = Integer.parseInt(x); return y; }else{
while(true){ System.out.print("你输入的值无意义! 请你重新输入: "); String num = sc.next();
if(isNumberic(num)==true){ int y1 = Integer.parseInt(num); return y1; } } } }
//判断字符串是否全是数字,是返回true,否则返回false public static boolean isNumberic(String str) {
//使用正则表达式进行判断是否全为数字 Pattern pattern = Pattern.compile("[0-9]*"); return
pattern.matcher(str).matches(); } }
2、图片显示工具类(PayView)
import javax.imageio.ImageIO; import javax.swing.*; import java.awt.*; import
java.io.File; /* *生成二维码 *下面代码只需要换一个图片即可 *new PayView();//创建这个对象即可展示支付图片 */
public class PayView extends JFrame{ 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("eighth\\src\\MathDemo\\name\\看了就要负责.jpg"));
g.drawImage(image, 0, 0, 1273, 809, null); } catch (Exception e) { // TODO
Auto-generated catch block e.printStackTrace(); } } }
二、登录界面的功能
/* 登录界面 */ import java.io.File; import java.io.FileReader; import
java.io.FileWriter; import java.io.IOException; import java.util.Properties;
import java.util.Random; import java.util.Scanner; public class loginView {
public static final Scanner sc=new Scanner(System.in); public static int
money=0; public static int count=1; //用户登录注册 public void Login() throws
InterruptedException, IOException { //循环判断 boolean flag=true; //定义文件 File f1;
File f2; //集合 Properties prop=new Properties(); //循环控制 while (flag){
System.out.println("请输入你的账户&密码"); System.out.print("账号:"); String
userName=sc.next(); //文件路径初始化 f1=new
File("eighth\\src\\MathDemo\\name",userName); //判断文件存在与否 if (!f1.exists()){
System.out.println("你输入的账户信息不存在,请选择是否注册(y/n)。"); char
c=TSUtility.readConfirmSelection(); if (c=='Y'){ System.out.print("账号为:");
String userName1=sc.next(); f2=new
File("eighth\\src\\MathDemo\\name",userName1); System.out.print("密码为:"); String
password1=sc.next(); prop.setProperty("Password",password1);
prop.setProperty("Money", String.valueOf(money)); prop.setProperty("Count",
String.valueOf(count)); FileWriter fw=new FileWriter(f2); //集合到文件
prop.store(fw,"------UserInfo------"); fw.close();
TSUtility.loadSpecialEffects(); System.out.println("注册成功。"); }else {
System.out.println("已取消注册,请问是否要退出系统(y/n)"); char
c1=TSUtility.readConfirmSelection(); if (c1=='Y'){ System.exit(0); }else {
System.out.println("请重新输入你的账户&密码"); } } }else { System.out.print("密码:"); String
pw=sc.next(); FileReader fr=new FileReader(f1); prop.load(fr); fr.close();
//判断是否正确 if (prop.getProperty("Password").equals(pw)){
System.out.println(userName + "欢迎你! " +userName+ "您目前余额为:" +
prop.getProperty("Money") + "元,免费次数为:" + prop.getProperty("Count")); flag =
false; //猜数小游戏,开始玩游戏 loginView.guessNumber(prop,f1); }else {
System.out.println("密码错误,请重新登录。"); } } } } //猜数字游戏 public static void
guessNumber(Properties prop, File f1) throws IOException { Scanner sc = new
Scanner(System.in); System.out.println("宇宙至尊游戏:猜数字(0-100)");
System.out.println("开始!"); while (true) { int count1 =
Integer.parseInt(prop.getProperty("Count")); int money1 =
Integer.parseInt(prop.getProperty("Money")); if (count1 <= 0 && money1 < 50) {
System.out.println("余额不足,请问是否充值?(y/n)"); char c =
TSUtility.readConfirmSelection(); if (c == 'Y') { addMoney(prop, f1); } else {
System.out.println("已退出充值,白嫖的早点圆润地爪巴。"); break; } } else { Random r = new
Random(); //生成随机数 int number = r.nextInt(101); System.out.println("测试使用:" +
number); while (true) { System.out.println("请输入你要猜的数字:"); int guessnumber =
sc.nextInt(); if (guessnumber > number) { System.out.println("你猜的数字" +
guessnumber + "大了"); } else if (guessnumber < number) {
System.out.println("你猜的数字" + guessnumber + "小了"); } else {
System.out.println("恭喜你猜中了"); //判断是否还有游戏次数 if (count1 != 0) { count1--; //次数减一
//改变集合中的count值 prop.setProperty("Count", String.valueOf(count1)); } else {
//判断是否有余额 if (money1 != 0) { //设置使用金额50元玩一次 money1 -= 50; //改变集合中的Money值
prop.setProperty("Money", String.valueOf(money1)); } } break; } } }
//集合中的内容写入到文件 FileWriter fw = new FileWriter(f1); prop.store(fw, null);
fw.close(); System.out.println("继续玩? y/n"); char c =
TSUtility.readConfirmSelection(); if (c == 'Y') { //重新获取一次值,为了保证获取到的是充值后的数据 int
count2 = Integer.parseInt(prop.getProperty("Count")); int money2 =
Integer.parseInt(prop.getProperty("Money")); //判断是否有玩的条件 if (count2 <= 0 &&
money2 < 50) { System.out.println("余额不足,是否充值y/n"); char c1 =
TSUtility.readConfirmSelection(); if (c1 == 'Y') { //充值模块 addMoney(prop, f1); }
else { System.out.println("已退出充值!"); break; } } } else {
System.out.println("已退出游戏!"); break; } } } //充值中心 private static void
addMoney(Properties prop,File f1) throws IOException { while (true){
System.out.println("¥¥¥¥ 充值系统 ¥¥¥¥"); System.out.println("¥¥ 充一百送5次 ¥¥");
System.out.println("¥¥ 充两百送12次 ¥¥"); System.out.println("¥¥ 充三百送18次 ¥¥");
System.out.println("¥ 充五百及以上送31次! ¥"); System.out.println("¥¥¥¥ 充值系统 ¥¥¥¥");
System.out.println("请输入您的充值金额:"); String s=sc.next(); int
money=TSUtility.printInt(s); while (true){ if (money != 100 && money != 200 &&
money != 300 && money < 500){ System.out.println("输入金额错误!请重新输入:"); String s1 =
sc.next(); int money1 = TSUtility.printInt(s1); money = money1; } else {
prop.setProperty("Money", String.valueOf(money)); if (money==100){
prop.setProperty("Count", String.valueOf(5)); }else if (money == 200){
prop.setProperty("Count", String.valueOf(12)); }else if (money == 300){
prop.setProperty("Count", String.valueOf(18)); }else {
prop.setProperty("Count", String.valueOf(31)); } FileWriter fw=new
FileWriter(f1); prop.store(fw,null); fw.close(); System.out.println("充值成功!");
break; } } break; } } //用户修改 public void revise() throws InterruptedException,
IOException { Properties prop=new Properties();
System.out.println("请输入你要修改的账户"); String userName=sc.next(); File file=new
File("eighth\\src\\MathDemo\\name",userName); if (!file.exists()){
System.out.println("你输入的账户信息不存在。"); }else { FileReader fr=new FileReader(file);
prop.load(fr); fr.close(); //修改密码 System.out.print("请输入你要修改的新密码(" +
"(旧密码:"+prop.getProperty("Password")+")" + "):"); String pw = sc.next();
//修改集合中的数据 prop.setProperty("Password", pw); //集合到文件 FileWriter fw = new
FileWriter(file); prop.store(fw, null); fw.close();
System.out.println("修改密码成功!"); System.out.println("当前账户:" + userName);
System.out.println("当前密码:" + pw); } } }
三、单独写的猜拳游戏类
import java.io.FileWriter; import java.io.IOException; import
java.util.Properties; import java.util.Random; import java.util.Scanner; public
class HandGame { public static void handGame() throws IOException {
System.out.println("规则如下:");
System.out.println("1代表剪刀、2代表石头、3代表布。剪刀<石头<布,石头<布<剪刀,布<剪刀<石头");
System.out.println("-------------------"); FileWriter fw=new
FileWriter("eighth\\src\\MathDemo\\name\\系统拳.txt"); fw.write("1=剪刀\r\n");
fw.write("2=石头\r\n"); fw.write("3=布\r\n"); fw.close(); Properties prop=new
Properties(); prop.setProperty("1","剪刀"); prop.setProperty("2","石头");
prop.setProperty("3","布"); while (true){ Scanner sc=new Scanner(System.in);
System.out.println("石头2、剪刀1、布3!!!给我比个手出来。laozi要数字。"); int hand=sc.nextInt();
Random r=new Random(); int i = r.nextInt(3)+1; if (hand==1 && i==1){
System.out.println("你出的是:"+prop.getProperty(String.valueOf(hand)));
System.out.println("系统出的是:"+prop.getProperty(String.valueOf(i)));
System.out.println("你居然跟系统拳打成平手?laji..."); }else if (hand==1 && i==2){
System.out.println("你出的是:"+prop.getProperty(String.valueOf(hand)));
System.out.println("系统出的是:"+prop.getProperty(String.valueOf(i)));
System.out.println("然后呢?你输了?ztmlaji..."); }else if (hand==1 && i==3){
System.out.println("你出的是:"+prop.getProperty(String.valueOf(hand)));
System.out.println("系统出的是:"+prop.getProperty(String.valueOf(i)));
System.out.println("这就赢了?啊tui,dtmlaji..."); }else if (hand==2 && i==1){
System.out.println("你出的是:"+prop.getProperty(String.valueOf(hand)));
System.out.println("系统出的是:"+prop.getProperty(String.valueOf(i)));
System.out.println("这就赢了?啊tui,dtmlaji..."); }else if (hand==2 && i==2){
System.out.println("你出的是:"+prop.getProperty(String.valueOf(hand)));
System.out.println("系统出的是:"+prop.getProperty(String.valueOf(i)));
System.out.println("你居然跟系统拳打成平手?laji..."); }else if (hand==2 && i==3){
System.out.println("你出的是:"+prop.getProperty(String.valueOf(hand)));
System.out.println("系统出的是:"+prop.getProperty(String.valueOf(i)));
System.out.println("然后呢?你输了?ztmlaji..."); }else if (hand==3 && i==1){
System.out.println("你出的是:"+prop.getProperty(String.valueOf(hand)));
System.out.println("系统出的是:"+prop.getProperty(String.valueOf(i)));
System.out.println("然后呢?你输了?ztmlaji..."); }else if (hand==3 && i==2){
System.out.println("你出的是:"+prop.getProperty(String.valueOf(hand)));
System.out.println("系统出的是:"+prop.getProperty(String.valueOf(i)));
System.out.println("这就赢了?啊tui,dtmlaji..."); }else if (hand==3 && i==3){
System.out.println("你出的是:"+prop.getProperty(String.valueOf(hand)));
System.out.println("系统出的是:"+prop.getProperty(String.valueOf(i)));
System.out.println("你居然跟系统拳打成平手?laji..."); }
System.out.println("-------------------"); System.out.println("还想继续玩?傻子吧还玩?
y/n"); char c = TSUtility.readConfirmSelection(); if (c != 'Y') {
System.out.println("已退出游戏!(早该走了...)"); break; } } } }
四、以上就是全部代码,这个游戏就这样。