A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

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);
                        }
                }
        }
}

评分

参与人数 1黑马币 +2 收起 理由
耀阳圣尊 + 2 继续努力,加油!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马