JAVA打字游戏demo

刚学JAVA,开始不知道写什么,先看的别人写的,原文链接
游戏类

package type_Game;

import java.util.Scanner;

public class Player {

    private int rank;   // 级别
    private int curScore;   // 分数

    public Player() {
    }

    public Player(int rank, int curScore) {
        this.rank = rank;   // 难度等级
        this.curScore = curScore;   // 分数
    }

    public void Score(){
        int flag = 1;
        String s = "";
        Scanner sc = new Scanner(System.in);
        while (flag == 1 && this.rank < 7){
//            随等级增加,难度增加
            for (int i = 0; i < this.rank; i++) {
                s+=(char)(Math.random()*26+'A');    //  获取A-Z
            }
            System.out.println("------请输入与下面相同的字母------");
            System.out.println("------"+s+"-------");
            long startTime = System.currentTimeMillis();    // 获取开始时间
            String letter = sc.next();
            long endTime = System.currentTimeMillis();  //  获取结束时间
//            判断输入的与字母是否相等,相等则加分并增加等级,继续循环
            if(s.equalsIgnoreCase(letter)){
                this.curScore += 10;
                System.out.println("------输入正确,你当前分数为"+this.curScore+"\n你当前等级为"+this.rank);
                System.out.println("共用"+(endTime-startTime)/1000+"秒");
                this.rank++;
                flag = 1;
                s = "";
                if (this.rank>7){
                    System.out.println("------你已通关------");
                }
            }
            else if (!s.equals(letter)){
                System.out.println("------输入错误,游戏结束------");
                flag = 0;
            }
        }

    }
}

用户类

package type_Game;

import java.util.Scanner;

public class User {
    private String name;
    private String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public void enter(){
        Scanner sc = new Scanner(System.in);
        System.out.println("------请输入姓名------");
        String name = sc.next();
        System.out.println("------请输入密码------");
        String password = sc.next();
        if(name.equals(this.name) && password.equals(this.password)){
            System.out.println("------欢迎你"+name+"游戏马上开始------");
        }
        else{
            System.out.println("姓名密码不对,请重新输入");
            enter();
        }
    }
}

主类

package type_Game;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        User us = new User("张三丰", "123123");
        us.enter();
        Player player = new Player(1, 0);
        player.Score();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值