本帖最后由 威威是超人 于 2016-9-9 11:21 编辑
import java.util.Scanner;
class User{
//用户的两个变量
private String username="威威是超人";
private String password="dw123456";
//构造函数私有化
//创建本类对象
private User(){}
private static User u=new User();
public static User getInstance(){
return u;
}
//set创建用户名
public void setUsername(String username){
this.username=username;
}
//set创建用户的密码
public void setPassword(String password){
this.password=password;
}
//get获取用户名
public String getUsername(){
return username;
}
//get获取用户密码
public String getPassword(){
return password;
}
}
//用户登录
class Login {
public static boolean login(User user){
boolean bl=false;
//User user=new User();
User user3=User.getInstance();
Scanner sc=new Scanner(System.in);
for (int x = 2; x >= 0; x--) {
System.out.println("请输入您的用户名");
String username1= sc.nextLine();
System.out.println("请输入密码");
String password = sc.nextLine();
if (username1.equals(user3.getUsername())&& password.equals(user3.getPassword())){
return true;
}else {
System.out.println("您还有"+x+"输入密码的机会");
}if (x<0){
break;
}
}
return false;
}
}
//用Math.random实现的猜字游戏
class PlayGame{
public void caiZi(){
int random=(int )(Math.random()*100) +1;
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("请输入一到一百的数");
int num=sc.nextInt();
if (num>random){
System.out.println("您输入的大了");
}else if(num<random){
System.out.println("您输入的小了");
}else if(num==random){
System.out.println("恭喜您猜对了");
System.out.println("请问您要继续游戏吗?1.继续 2.退出 请输入");
int num1=sc.nextInt();
if(num1==1){
continue;
}else if(num1==2){
break;
}else{
System.out.println("您输入有误");
}
}
}
}
}
class ChangePassword{
public static void changePassword(){
Scanner sc=new Scanner(System.in);
User user1=User.getInstance();
System.out.println("请输入您更改后的密码");
String password3=sc.nextLine();
user1.setPassword(password3);
}
}
class Register{
public static void register(){
Scanner sc=new Scanner(System.in);
User user2=User.getInstance();
//请输入用户名
System.out.println("请输入要注册的用户名");
String username2=sc.nextLine();
user2.setUsername(username2);
//请输入密码
System.out.println("请输入您的初始密码");
String password2=sc.nextLine();
user2.setPassword(password2);
}
}
class TestDemo {
//用户登陆系统,玩猜字游戏
public static void main(String[] args) {
System.out.println("欢迎进入本系统");
System.out.println("1:注册 2:登陆");
System.out.println("3:修改密码 4:退出");
System.out.println("请输入你的选择:");
Scanner sc = new Scanner(System.in);
int select = sc.nextInt();
switch(select){
case 1:
//注册代码
Register.register();
case 2:
User user =User.getInstance();
boolean bl = Login.login(user);
if (bl == true) { //如果登陆成功
new PlayGame().caiZi(); //玩猜字游戏
} else {
System.out.println("账户和密码错误超过3次,不好意思,系统将退出");
}
break;
case 3:
//修改密码
ChangePassword.changePassword();
default:
break;
}
}
}
|
|