黑马程序员技术交流社区

标题: 自个写了个用户密码注册登录,高手指点点 [打印本页]

作者: yangshaoqiang    时间: 2015-10-27 23:44
标题: 自个写了个用户密码注册登录,高手指点点
public class Demo5 {
        static Scanner sc=new Scanner(System.in);
        public static void main(String[] args) throws IOException {
                System.out.println("请选择注册(1)或登录(2)");
                while(true){
                        int select=sc.nextInt();
                        if(select==1){
                                register();
                        }else if(select==2){
                                entry();
                        }else{
                                System.out.println("请选择注册(1)或登录(2)");
                        }
                }
               
        }
        public static void register() throws IOException{
                try(FileWriter fw= new FileWriter("e:\\welcom.txt")) {
                        System.out.println("请输入注册用户名");
                        String user=sc.next();
                        System.out.println("请输入注册密码");
                        String password=sc.next();
                        fw.write(user+"="+password);
                } catch (IOException e) {
                        e.printStackTrace();
                }
                System.out.println("储存成功");
                System.out.println("请选择登录(1)或退出(0)");
                        int select=sc.nextInt();
                        if(select==1){
                                entry();
                        }else if(select==0){
                                System.exit(0);
                        }
        }
        public static void entry() throws IOException{
                FileReader fr=new FileReader("e:\\welcom.txt");
                BufferedReader br=new BufferedReader(fr);
                System.out.println("请输入登录用户名");
                String user=sc.next();
                System.out.println("请输入登录密码");
                String password=sc.next();
                String key=user+"="+password;
                String str=br.readLine();
                while(true){
                        if(key.equals(str)){
                                System.out.println("Welcome!!");
                                break;
                        }else{
                                System.out.println("用户名或密码有误");
                                System.out.println("请重新登录系统");
                                System.exit(0);
                        }
                }
        }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2