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