- /**
- * 这是一个人模拟登陆类
- *
- * @author
- *
- */
- public class landingAnalog {
- private String username;
- private String password;
- public landingAnalog() {
- super();
- // TODO Auto-generated constructor stub
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- /**
- * 比较方法
- *
- * @param username
- * 用户名
- * @param password
- * 密码
- * @return 是否登陆成功
- */
- public boolean compare(String username, String password) {
- return this.username.equals(username) && this.password.equals(password);
- }
- }
- [code]
- import java.util.Scanner;
- public class landingAnalogDemo {
- public static void main(String[] args) {
- landingAnalog la = new landingAnalog();
- la.setUsername("admin");
- la.setPassword("admin");
- Scanner sc = new Scanner(System.in);
- for (int x = 0; x < 3; x++) {
- System.out.println("请输入您要登陆的用户名:");
- String name = sc.nextLine();
- System.out.println("请输入您 的登陆密码:");
- String pwd = sc.nextLine();
- if (la.compare(name, pwd)) {
- System.out.println("恭喜您,登陆成功。");
- System.out.println("欢迎来玩耍猜数小游戏");
- // 产生随机数
- int sys = (int) (Math.random() * 100) + 1;
- for (x = 0; x < 5; x++) {
- System.out.println("请输入1-100之间的数字,来猜猜我们的默契!!!");
- int num = sc.nextInt();
- if (num < 1 || num > 100) {
- System.out.println("输入有误,请按规则来玩耍");
- } else {
- if (num > sys) {
- System.out.println("没默契,猜大了,好伤心。" + "你还有" + (4 - x)
- + "次机会爻!!!");
- } else if (num < sys) {
- System.out.println("没默契,猜小了,好伤心。" + "你还有" + (4 - x)
- + "次机会爻!!!");
- } else if (num == sys) {
- System.out.println("恭喜你猜中了,去苍老师处领取我们的\"surprise\"!");
- break;
- }
- if ((4 - x) == 0) {
- System.out.println("亲,您已猜错超过5次。本次玩耍结束" + "\n"
- + "亲,我的数是:" + sys);
- }
- }
- }
- } else {
- if ((2 - x) == 0) {
- System.out.println("您本次登陆错误次数已达上限,账号已锁定。请明天再来");
- } else {
- System.out.println("您输入的用户名已密码有误,还有" + (2 - x) + "机会。");
- }
- }
- }
- }
- }
复制代码 [/code] |