赞
踩
A:模拟用户登录,给3次机会,并提示还有几次;
B:登录成功后,可选择是否玩猜数字小游戏;
A:定义用户名和密码(假设已经存在);
B:键盘录入用户名和密码;
C:比较A,B中的用户名密码,若匹配则登录成功,并选择是否开始游戏,否则登录失败;
D:给3次机会,用循环改进;
- package String_Realize;
-
- import java.util.Scanner;
-
- /*
- * 需求:实现猜数字小游戏
- *
- * 分析:
- * A:由系统随机给定数字,范围是1-100;
- * B:键盘录入要猜的数字,给10次机会;
- */
- public class GuessNumber {
-
- // 猜数字方法
- public static void start() {
- // 记录系统随机给出的数字
- int number = (int) (Math.random() * 100 + 1);
-
- // 键盘录入要猜的数字,给10次机会
- Scanner sc = new Scanner(System.in);
- for (int i = 0; i < 10; i++) {
- System.out.println("请输入你猜的数字(1-100):");
- int guess = sc.nextInt();
- if (guess == number) {
- System.out.println("恭喜你第" + (i + 1) + "次猜中了!");
- break;
- } else {
- if (i == 9) {
- System.out.println("珍爱生命,告别猜谜!");
- break;
- }
- if (guess > number) {
- System.out.println("你猜的数字大了!,还有" + (9 - i) + "次机会");
- } else if (guess < number) {
- System.out.println("你猜的数字小了!,还有" + (9 - i) + "次机会");
- }
- }
- }
-
- }
- }

- package String_Realize;
-
- import java.util.Scanner;
-
- /*
- * 需求:
- * A:模拟用户登录,给3次机会,并提示还有几次;
- * B:登录成功后,可选择是否玩猜数字小游戏;
- * 分析:
- * A:定义用户名和密码(假设已经存在);
- * B:键盘录入用户名和密码;
- * C:比较A,B中的用户名密码,若匹配则登录成功,否则登录失败
- * D:给3次机会,用循环改进;
- */
- public class StringTest {
- public static void main(String[] args) {
- // 定义用户名和密码
- String username = "admin";
- String password = "admin";
-
- // 键盘录入用户名和密码
- Scanner sc = new Scanner(System.in);
-
- // 比较用户名和密码,只给3次机会
- for (int i = 0; i < 3; i++) {
-
- System.out.println("请输入用户名:");
- String name = sc.nextLine();
- System.out.println("请输入密码:");
- String words = sc.nextLine();
-
- if (name.equals(username) && words.equals(password)) {
- System.out.println("登录成功!是否进行猜数字小游戏?(Y/N)");
- while (true) {
- String choice = sc.nextLine();
- if (choice.equals("Y") || choice.equals("y")) {
- GuessNumber.start();
- System.out.println("你还要玩吗?(Y/N)");
- } else {
- break;
- }
- }
- break;
- } else {
- if (i == 2) {
- System.out.println("账号已冻结!");
- break;
- }
- System.out.println("用户名或密码错误,你还有" + (2 - i) + "次机会!");
- }
- }
- }
- }

A:有哪些类呢?
用户类
测试类
B:每个类有哪些东西呢?
a:用户类:
成员变量:用户名 ,密码
构造方法:无参构造
成员方法:getXxx()/setXxx()
登录、注册
b:测试类:main方法
c:为了更清晰的分类,把用户又划分成两类
用户基本描述类
成员变量:用户名 ,密码
构造方法:无参构造
成员方法:getXxx()/setXxx()
用户操作类
登录、注册
C:类与类之间的关系是什么呢?
在测试类中创建用户操作类和基本描述类的对象,并使用其功能;
D:分包按照功能划分
a:用户基本描述类包cn.itcast.pojo
b:用户操作接口cn.itcast.dao
c:用户操作类包cn.itcast.dao.impl
d:用户测试类cn.itcast.test
注意:由于猜数字类已在上方给出,下面将不再给出;
- package cn.itcast.pojo;
-
- /**
- * 这是用户基本描述类
- *
- * @author Administrator
- * @version V1.0
- */
- public class User {
- // 用户名和密码
- private String username;
- private String password;
-
- // 无参构造
- public User() {
-
- }
-
- // 成员方法
- 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;
- }
-
- }

- package cn.itcast.dao;
-
- import cn.itcast.pojo.User;
-
- /**
- * 这是用户操作类接口
- * @author Administrator
- * @version V1.0
- */
- public interface UserDao {
- //登录功能
- public abstract boolean islogin(String username,String password);
- //注册功能,直接传对象过来
- public abstract void regist(User user);
- }
- package cn.itcast.dao.impl;
-
- import java.util.ArrayList;
-
- import cn.itcast.dao.UserDao;
- import cn.itcast.pojo.User;
-
- /**
- * 这是用户操作的具体实现类(集合版)
- *
- * @author Administrator
- * @version V1.0
- */
- public class UserDaoImpl implements UserDao {
- // 将集合定义为成员变量
- private static ArrayList<User> array = new ArrayList<User>();
-
- public boolean islogin(String username, String password) {
- // 遍历集合,获取每个用户,并判断用户名和密码是否匹配
- boolean flag = false;
- for (User user : array) {
- if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
- flag = true;
- break;
- }
- }
- return flag;
- }
-
- public void regist(User user) {
- // 将注册的信息存入集合,将集合看作服务器
-
- array.add(user);
- }
-
- }

- package cn.itcast.test;
-
- import java.util.Scanner;
-
- import cn.itcast.dao.UserDao;
- import cn.itcast.dao.impl.UserDaoImpl;
- import cn.itcast.pojo.User;
-
- public class TestDemo {
- public static void main(String[] args) {
- while (true) {
- // 欢迎界面,给出选项
- System.out.println("--------------欢迎光临--------------");
- System.out.println("1登录");
- System.out.println("2注册");
- System.out.println("3退出");
- System.out.println("请输入你的选择:");
-
- // 键盘录入选择,为了后面录入信息方便,所有数据录入全部用字符串接受
- Scanner sc = new Scanner(System.in);
- String choiceString = sc.nextLine();
-
- // 多个地方要使用,就定义到外面
- UserDao ud = new UserDaoImpl();
-
- // 经过思考选择了switch
- switch (choiceString) {
- case "1":
- // 登录界面
- System.out.println("--------------登录界面--------------");
- System.out.println("请输入用户名:");
- String username = sc.nextLine();
- System.out.println("请输入密码:");
- String password = sc.nextLine();
-
- // 调用登录功能
- boolean flag = ud.islogin(username, password);
-
- if (flag) {
- System.out.println("登录成功,可以玩游戏了!");
- System.out.println("你玩游戏吗?y/n");
- while (true) {
- String resultString = sc.nextLine();
- if (resultString.equalsIgnoreCase("y")) {
- GuessNumber.start();
- System.out.println("你还玩吗?y/n");
- } else {
- break;
- }
-
- }
- System.out.println("谢谢使用,欢迎下次再来");
- System.exit(0);
-
- } else {
- System.out.println("用户名或者密码有误,登录失败");
- }
-
- break;
- case "2":
- // 注册界面
- System.out.println("--------------注册界面--------------");
- System.out.println("请输入用户名:");
- String newUsername = sc.nextLine();
- System.out.println("请输入密码:");
- String newPassword = sc.nextLine();
-
- // 将用户名与密码封装到对象中
- User user = new User();
- user.setUsername(newUsername);
- user.setPassword(newPassword);
-
- // 调用注册功能,多态用法
- ud.regist(user);
- System.out.println("注册成功!");
- break;
- case "3":
- default:
- System.out.println("谢谢使用,欢迎下次再来");
- System.exit(0);
- break;
- }
- }
- }
- }

A:集合版的问题是不能记忆注册后的账号和密码,每次运行程序,都会导致重置;
B:使用IO流,将注册的信息写入到文件中,每次登录的时候,取出文件的信息进行匹配;
C:关于测试类,猜数字类,用户基本信息类和用户操作类接口无须更改,只需在实现用户操作类接口时,
将登录和注册操作用IO流实现;
- package cn.itcast.dao.impl;
-
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
-
- import cn.itcast.dao.UserDao;
- import cn.itcast.pojo.User;
-
- /**
- * 这是用户操作的具体实现类(IO版)
- *
- * @author 风清扬
- * @version V1.1
- *
- */
- public class UserDaoImpl implements UserDao {
-
- // 程序加载就创建文件,使用静态代码块
- private static File file = new File("user.txt");
- static {
- try {
- file.createNewFile();
- } catch (IOException e) {
- System.out.println("创建文件失败");
- // e.printStackTrace();
- }
- }
-
- @Override
- public boolean isLogin(String username, String password) {
- boolean flag = false;
- BufferedReader br = null;
- try {
- br = new BufferedReader(new FileReader(file));
- String line = null;
- while ((line = br.readLine()) != null) {
- // 得到用户名=密码
- String[] datas = line.split("=");
- if (datas[0].equals(username) && datas[1].equals(password)) {
- flag = true;
- break;
- }
- }
- } catch (FileNotFoundException e) {
- System.out.println("找不到信息所在的文件");
- } catch (IOException e) {
- System.out.println("用户登录失败");
- // e.printStackTrace();
- } finally {
- if (br != null) {
- try {
- br.close();
- } catch (IOException e) {
- System.out.println("用户登录释放资源失败");
- }
- }
- }
-
- return flag;
- }
-
- @Override
- public void regist(User user) {
- /*
- * 为了让注册的数据能够有一定的规则,自己定义一个规则: 用户名=密码(用等号隔开,目的是一次读一行,然后分割)
- */
- BufferedWriter bw = null;
- try {
- // 为了保证数据追加写入
- bw = new BufferedWriter(new FileWriter(file, true));
- bw.write(user.getUsername() + "=" + user.getPassword());
- bw.newLine();
- bw.flush();
- } catch (IOException e) {
- System.out.println("用户注册失败");
- // e.printStackTrace();
- } finally {
- // 防止空指针异常
- if (bw != null) {
- try {
- bw.close();
- } catch (IOException e) {
- System.out.println("用户注册释放资源失败");
- // e.printStackTrace();
- }
- }
- }
- }
-
- }

赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。