作为系统,构造初始模型分为两部分,一是构建数据库,连接各种指令,另一是完成各种指令的具体操作。
所以在这里,不妨设置两个类,一包括系统登陆后面板及所需各种功能,一为面向用户的各种指令调配。即在声明第二个类能调用第一类的各种函数的情况下只需运行第二个类即可使系统满足用户的基本需求。
在第一个类中储存所需基本信息。
static String userName;
static String password;
String address;
String phone;
static int Loginnumber;
int score;
static boolean islogin;
public void showInfo()
{
System.out.println("用户信息:");
System.out.println("用户名:"+userName);
System.out.println("密码:"+password);
System.out.println("地址:"+address);
System.out.println("登录次数:"+Loginnumber);
System.out.println("当前积分:"+score);
System.out.println("是否登录:"+(islogin?"是":"否"));
System.out.println("------------------");
}
再根据第二类所设置的各种功能添加各种代码。
public static int login(String pwdIn)
{
if (checkpassword(pwdIn))
{
if (islogin)
{
System.out.println("您已登录");
return 2;
}
{
islogin=true;
Loginnumber++;
System.out.println(userName+"登录成功");
return 0;
}
}
{
System.out.println(userName+"登陆失败");
return 1;
}
}
public static boolean checkpassword(String pwdIn)
{
if(pwdIn.equals (password))
{
System.out.println(userName+"验证成功");
return true;
}
System.out.println(userName +"验证失败");
return false;
}
public void logOut()
{
islogin=false;
System.out.println(userName+"已退出");
}
public void changePassword(String oldPwd,String newPwd)
{
if(checkpassword(oldPwd))
{
password=newPwd;
System.out.println(userName+"修改成功");
}
else
{
System.out.println("修改失败");
}
}
public void changeScore()
{
Random r1=new Random();
if (r1.nextInt()%2==1)
{
score-=r1.nextInt();
}
else
{
int a;
a=r1.nextInt()*2;
score+=a;
}
System.out.println("当前积分为:"+score);
}
public void changename(String newName)
{
userName=newName;
}
第二类则编写所想在系统中加的功能,对用户动作的监听也在这一类。
public static void main(String[] args)
{
User user1 = new User();
user1.userName = "图图";
user1.password = "123456";
user1.address = "翻斗花园";
user1.Loginnumber = 0;
user1.score = 50;
user1.showInfo();
User user2 = new User();
user2.showInfo();
Scanner sc = new Scanner(System.in);
User user = new User();
while (true)
{
System.out.println("1.登录;2.注册;3.退出");
String choice = sc.nextLine();
if (choice.equals("1"))
{
System.out.println("请输入密码:");
String pwd = sc.nextLine();
int logincode =User.login(pwd);
if (logincode == 0)
{
user.showInfo();
while(true)
{
System.out.println ("请选择需要使用的功能:0.修改姓名 1.修改密码 2.积分抽奖 3.退出登录");
String cho1 = sc.nextLine ();
if(cho1.equals ("0"))
{
System.out.println("请输入新名字:");
String newName = sc.nextLine();
user.changename(newName);
}
else if(cho1.equals ("1"))
{
System.out.println ("请输入旧密码:");
String oldPwd = sc.nextLine ();
System.out.println ("请输入新密码:");
String newPwd = sc.nextLine ();
user.changePassword (oldPwd, newPwd);
}
else if(cho1.equals ("2"))
{
user.changeScore();
}
else if(cho1.equals ("3"))
{
user.logOut ();
break;
}
}
}
else if(logincode == 1)
{
System.out.println ("密码错误");
}
else if(logincode == 2)
{
System.out.println ("已经登录");
}
}
else if(choice.equals ("2"))
{
System.out.println ("请输入用户名:");
user.userName = sc.nextLine ();
System.out.println ("请输入密码:");
user.password = sc.nextLine ();
System.out.println ("请输入地址:");
user.address = sc.nextLine ();
System.out.println ("请输入电话:");
user.phone = sc.nextLine ();
System.out.print ("注册成功");
user.showInfo ();
}
else if(choice.equals ("3"))
{
System.out.println ("退出系统");
System.exit (0);
break;
}
}
}
编写代码时可能遇上静动态变量不匹配的问题,将其调整使其匹配就好了。