实现用户注册与登录功能

实现用户注册与登录功能

(1)注册:将用户信息保存到磁盘

登录:将用户信息从磁盘中读取出来,并输出到控制台

package test2;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class testU {

	public static void main(String[] args) throws IOException {
		boolean flag=true;
		while(flag) {
		System.out.println("请选择功能1=注册,2=登录,3=退出");
		int a=new Scanner(System.in).nextInt();
		switch(a) {
		case 1:
			System.out.print("请输入用户名");
			String name=new Scanner(System.in).next();
			System.out.print("请输入密码");
			String password=new Scanner(System.in).next();
			
			//创建并实例化FileWriter对象
			FileWriter f=new FileWriter("D:\\上课\\"+name+".txt");
			f.write(password);
			f.close();
			System.out.println("注册成功");
			break;
		case 2:
			File f2=new File("D:\\上课");
			File[] fs = f2.listFiles();
			System.out.println("请输入用户名");
			String name1=new Scanner(System.in).next();
			//遍历该文件夹下的所有文件判断是否存在该用户
			boolean isok=false;
			for(File s:fs) {
				if((name1+".txt").equals(s.getName())) {
					isok=true;
				}
			}
			if(isok==true) {
				System.out.println("请输入密码");
				String pass2=new Scanner(System.in).next();
				FileReader f3=new FileReader("D:\\上课\\"+name1+".txt");
				int k=0;
				String str="";
				while((k=f3.read())!=-1) {
					str+=(char)k;
				}
				f3.close();
				if(pass2.equals(str)) {
					System.out.println("登录成功");
				}else {
					System.out.println("密码错误,请重新输入");
				}
			}else {
				System.out.println("此用户尚未注册,请先注册再登录");
			}
			break;
		case 3:
			System.out.println("退出程序");
			flag=false;
			break;
		default :
			System.out.println("输入有误,请重新输入");
			break;
		}
		}
	}
}

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,用户登录页面通常包括用户名、密码输入框和登录按钮,可以使用 HTML 和 CSS 来编写。 其,HTML 代码可以这样写: ```html <!DOCTYPE html> <html> <head> <title>用户登录</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> /* 在这里编写 CSS 样式 */ </style> </head> <body> <div class="login-form"> <h2>用户登录</h2> <form action="#" method="post"> <label for="username">用户名:</label> <input type="text" id="username" name="username" placeholder="请输入用户名" required> <label for="password">密码:</label> <input type="password" id="password" name="password" placeholder="请输入密码" required> <button type="submit">登录</button> </form> </div> </body> </html> ``` 然后在 `<style>` 标签编写 CSS 样式,例如: ```css .login-form { margin: 100px auto; padding: 20px; max-width: 400px; border: 1px solid #ccc; border-radius: 5px; box-shadow: 0 0 5px rgba(0, 0, 0, 0.1); text-align: center; } .login-form h2 { margin-top: 0; } .login-form form { display: inline-block; text-align: left; } .login-form label { display: block; margin-bottom: 10px; } .login-form input[type="text"], .login-form input[type="password"] { display: block; width: 100%; padding: 10px; margin-bottom: 20px; border: 1px solid #ccc; border-radius: 5px; box-sizing: border-box; } .login-form button[type="submit"] { display: block; width: 100%; padding: 10px; background-color: #428bca; color: #fff; border: none; border-radius: 5px; cursor: pointer; } ``` 这样就可以得到一个简单的用户登录页面了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值