这次用java和C#来实现账号登录等功能,因为我感觉它们挺像的,所以就干脆一起来吧。在python中可以通过列表(list)和字典(dict)来存储实现,在java中可以通过ArrayList来实现账号密码的存储和判断,在C#中可以通过泛型List来存储。只要能找到存储账号和密码的途径,剩下的就好办了,话不多说,详细说明请看注释,直接列出代码。
Java源代码如下:
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;
//这里我另外创建了一个类来实现
class Account
{
private static final Scanner scn = new Scanner(System.in);
//设置一个公共的Scanner,不用重复声明定义
private static final ArrayList<String> user = new ArrayList<>();
//设置一个user列表来存储账号
private static final ArrayList<String> pwd = new ArrayList<>();
//设置一个pwd列表存储密码
private static final ArrayList<String> userpwd = new ArrayList<>();
//设置一个列表userpwd来存储账号+密码以及验证账号密码是否匹配
protected void Login() //账号登录
{
System.out.println("\n----------------- 1.账号登录 ------------------");
System.out.print("请输入账号:");
String username = scn.nextLine().trim();
System.out.print("请输入密码:");
String password = scn.nextLine().trim();
while (!Objects.equals(username, "") && !Objects.equals(password, ""))
{
if (Objects.equals(username, ""))
{
System.out.println("账号不得为空!\n请输入账号:");
username = scn.nextLine().trim();
}
else if (Objects.equals(password, ""))
{
System.out.print("账号密码不得为空!\n请输入密码:");
password = scn.nextLine().trim();
}
else if (!user.contains(username))
{
System.out.println("该账号不存在!是否选择注册一个新账号!\n1、是;2、否.");
String num = scn.nextLine().trim();
if (Objects.equals(num, "1"))
{
Register();
}
else if (Objects.equals(num, "2"))
{
System.out.println();
Main2();
}
}
else if (userpwd.contains(username + password))
{
System.out.println("账号登录成功!\n");
Main2();
}
else
{
System.out.println("账号密码错误!");
Login();
}
}
}
private void Register() //账号注册
{
System.out.println("\n----------------- 2.账号注册 ------------------");
System.out.print("请输入账号:");
String username = scn.nextLine().trim();
System.out.print("请输入密码:");
String password = scn.nextLine().trim();
System.out.print("请确认密码:");
String repassword = scn.nextLine().trim();
while (!Objects.equals(username, "") && !Objects.equals(password, ""))
{
if (Objects.equals(username, ""))
{
System.out.println("注册账号不得为空!\n请输入账号:");
username = scn.nextLine().trim();
}
else if (Objects.equals(password, ""))
{
System.out.print("账号密码不得为空!\n请输入密码:");
password = scn.nextLine().trim();
}
else if (!Objects.equals(password, repassword) || Objects.equals(repassword, ""))
{
System.out.print("请重新确认密码:");
repassword = scn.nextLine().trim();
}
else if (user.contains(username))
{
System.out.println("该账号已注册!");
Register();
}
else
{
System.out.println("账号注册成功!\n");
user.add(username);
pwd.add(password);
userpwd.add(username + password);
Main2();
}
}
}
private void Change() //修改账号密码
{
System.out.println("\n----------------- 3.修改密码 ------------------");
System.out.print("请输入账号:");
String username = scn.nextLine().trim();
System.out.print("请输入旧密码:");
String oldpassword = scn.nextLine().trim();
System.out.print("请输入新密码:");
String newpassword = scn.nextLine().trim();
while (!Objects.equals(username, "") && !Objects.equals(oldpassword, ""))
{
if (Objects.equals(username, ""))
{
System.out.print("账号不得为空!\n请输入账号:");
username = scn.nextLine().trim();
}
else if (Objects.equals(oldpassword, ""))
{
System.out.print("密码不得为空!\n请输入旧密码:");
oldpassword = scn.nextLine().trim();
}
else if (Objects.equals(newpassword, ""))
{
System.out.print("密码不得为空!\n请输入新密码:");
newpassword = scn.nextLine().trim();
}
else if (Objects.equals(oldpassword, newpassword))
{
System.out.print("新旧密码相同!\n请重新确认新密码:");
newpassword = scn.nextLine().trim();
}
else if (!user.contains(username))
{
System.out.println("账号不存在!");
Change();
}
else if (userpwd.contains(username + oldpassword))
{
System.out.println("账号密码修改成功!\n");
for (int i = 0; i < pwd.size(); i++)
{
if (oldpassword.equals(pwd.get(i)))
{
pwd.remove(pwd.get(i));
}
}
for (int j = 0; j < userpwd.size(); j++)
{
if ((username + oldpassword).equals(userpwd.get(j)))
{
userpwd.remove(userpwd.get(j));
}
}
userpwd.add(username + newpassword);
Main2();
}
else
{
System.out.println("账号密码错误!");
Change();
}
}
}
private void Delete() //账号注销
{
System.out.println("\n----------------- 4.账号注销 ------------------");
System.out.print("请输入账号:");
String username = scn.nextLine().trim();
System.out.print("请输入密码:");
String password = scn.nextLine().trim();
while (!Objects.equals(username, "") && !Objects.equals(password, ""))
{
if (Objects.equals(username, ""))
{
System.out.print("账号不得为空!\n请输入账号:");
username = scn.nextLine().trim();
}
else if (Objects.equals(password, ""))
{
System.out.print("密码不得为空!\n请输入密码:");
password = scn.nextLine().trim();
}
else if (!user.contains(username))
{
System.out.println("账号不存在!");
Delete();
}
else if (userpwd.contains(username + password))
{
System.out.println("账号注销成功!");
for (int i = 0; i < user.size(); i++)
{
if (Objects.equals(username, user.get(i)))
{
user.remove(user.get(i));
}
}
for (int j = 0; j < pwd.size(); j++)
{
if (password.equals(pwd.get(j)))
{
pwd.remove(pwd.get(j));
}
}
for (int k = 0; k < userpwd.size(); k++)
{
if ((username + password).equals(userpwd.get(k)))
{
userpwd.remove(userpwd.get(k));
}
}
Main2();
}
else
{
System.out.println("账号密码错误!");
Delete();
}
}
}
public void Main2() //主程序选择功能
{
System.out.println("*********************************************************************");
System.out.println(" 1、账号登录;2、账号注册;3、修改密码;4、注销账号;0、退出程序. ");
System.out.println("*********************************************************************");
System.out.print("请输入选择:");
String num = scn.nextLine().trim();
while (true)
{
switch (num)
{
case "1" -> Login();
case "2" -> Register();
case "3" -> Change();
case "4" -> Delete();
case "0" ->
{
System.out.println("程序已退出!");
System.exit(0);
}
default ->
{
System.out.println("请重新选择:");
num = scn.nextLine().trim();
}
}
}
}
}
public class main3
{
public static void main(String[] args)
{
Account st = new Account();
st.Main2();
}
}
运行效果截图
然后是C#源代码:
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace New3
{
class Account
{
private static readonly List<string> user = new List<string>();
private static readonly List<string> pwd = new List<string>();
private static readonly List<string> userpwd = new List<string>();
private void Login()
{
Console.WriteLine("\n-------------- 1.账号登录 --------------");
Console.WriteLine("请输入账号:");
string username = Console.ReadLine().Trim();
Console.WriteLine("请输入密码:");
string password = Console.ReadLine().Trim();
while (true)
{
if (username.Equals(""))
{
Console.WriteLine("账号不得为空!\n请输入账号:");
username = Console.ReadLine();
}
else if (password.Equals(""))
{
Console.WriteLine("密码不得为空!请输入密码:");
password = Console.ReadLine();
}
else if (!user.Contains(username))
{
Console.WriteLine("账号不存在!");
YesOrNo();
}
else if (userpwd.Contains(username + password))
{
Console.WriteLine("账号登录成功!\n");
user.Add(username);
pwd.Add(password);
userpwd.Add(username + password);
Main2();
}
else
{
Console.WriteLine("账号密码错误!");
Login();
}
}
}
private void YesOrNo()
{
Console.WriteLine("是否选择注册一个新账号?1、是;2、否");
string num = Console.ReadLine().Trim();
while (true)
{
switch (num)
{
case "1": Register(); break;
case "2":
{
Console.WriteLine(); Main2();
}; break;
default:
{
Console.Write("请重新选择:");
num = Console.ReadLine().Trim();
}; break;
}
}
}
private void Register() //账号注册
{
Console.WriteLine("\n-------------- 2.账号注册 --------------");
Console.WriteLine("请输入新账号:");
string username = Console.ReadLine().Trim();
Console.WriteLine("请输入密码:");
string password = Console.ReadLine().Trim();
Console.WriteLine("请确认密码:");
string repasword = Console.ReadLine().Trim();
while (true)
{
if (username.Equals(""))
{
Console.WriteLine("账号不得为空!\n请输入账号:");
username = Console.ReadLine().Trim();
}
else if (password.Equals(""))
{
Console.WriteLine("密码不得为空!\n请输入密码:");
password = Console.ReadLine().Trim();
}
else if (repasword.Equals("") || !repasword.Equals(password))
{
Console.WriteLine("请重新确认密码:");
repasword = Console.ReadLine().Trim();
}
else if (user.Contains(username))
{
Console.WriteLine("该账号已注册!");
Register();
}
else
{
Console.WriteLine("账号注册成功!\n");
user.Add(username);
pwd.Add(password);
userpwd.Add(username + password);
Main2();
}
}
}
private void Change() //修改密码
{
Console.WriteLine("\n-------------- 3.修改密码 --------------");
Console.WriteLine("请输入账号:");
string username = Console.ReadLine().Trim();
Console.WriteLine("请输入旧密码:");
string oldpassword = Console.ReadLine().Trim();
Console.WriteLine("请输入旧密码:");
string newpassword = Console.ReadLine().Trim();
while (true)
{
if (username.Equals(""))
{
Console.WriteLine("账号不得为空!\n请输入账号:");
username = Console.ReadLine().Trim();
}
else if (oldpassword.Equals(""))
{
Console.WriteLine("密码不得为空!请输入密码:");
oldpassword = Console.ReadLine().Trim();
}
else if (newpassword.Equals("") || !newpassword.Equals(oldpassword))
{
Console.WriteLine("请重新输入新密码:");
newpassword = Console.ReadLine().Trim();
}
else if (!user.Contains(username))
{
Console.WriteLine("账号不存在!");
YesOrNo();
}
else if (userpwd.Contains(username + oldpassword))
{
Console.WriteLine("账号密码修改成功!\n");
for (int i = 0; i < pwd.Count; i++) //删除旧密码
{
if (oldpassword.Equals(pwd[i]))
pwd.Remove(pwd[i]);
}
for (int j = 0; j < userpwd.Count; j++) //删除账号+旧密码
{
if ((username + oldpassword).Equals(userpwd[j]))
userpwd.Remove(userpwd[j]);
}
userpwd.Add(username + newpassword);
Main2();
}
else
{
Console.WriteLine("账号密码错误!");
Login();
}
}
}
private void Delete() //账号注销
{
Console.WriteLine("\n-------------- 4.账号注销 --------------");
Console.WriteLine("请输入账号:");
string username = Console.ReadLine().Trim();
Console.WriteLine("请输入旧密码:");
string password = Console.ReadLine().Trim();
while (true)
{
if (username.Equals(""))
{
Console.WriteLine("账号不得为空!\n请输入账号:");
username = Console.ReadLine().Trim();
}
else if (password.Equals(""))
{
Console.WriteLine("密码不得为空!\n请输入密码:");
password = Console.ReadLine().Trim();
}
else if (!user.Contains(username))
{
Console.WriteLine("账号不存在!");
YesOrNo();
}
else if (userpwd.Contains(username + password))
{
Console.WriteLine("账号注销成功!\n");
for (int i = 0; i < user.Count; i++)
{
if (username.Equals(user[i]))
user.Remove(user[i]);
}
for (int j = 0; j < pwd.Count; j++)
{
if (password.Equals(pwd[j]))
pwd.Remove(pwd[j]);
}
for (int k = 0; k < userpwd.Count; k++)
{
if ((username + password).Equals(userpwd[k]))
userpwd.Remove(userpwd[k]);
}
Main2();
}
else
{
Console.WriteLine("账号密码错误!");
Main2();
}
}
}
public void Main2()
{
Console.WriteLine("***********************************************************************");
Console.WriteLine("1、账号登录;2、账号注册;3、修改密码;4、账号注销;0、退出程序.");
Console.WriteLine("***********************************************************************");
Console.Write("请输入选择:");
string num = Console.ReadLine().Trim();
while (true)
{
switch (num)
{
case "1": Login(); break;
case "2": Register(); break;
case "3": Change(); break;
case "4": Delete(); break;
case "0":
{
Console.WriteLine("程序已退出!");
Process.GetCurrentProcess().Kill();
}; break;
default:
{
Console.WriteLine("请重新输入选择:");
num = Console.ReadLine().Trim();
}; break;
}
}
}
}
class Program
{
static void Main()
{
Account st = new Account();
st.Main2();
Console.ReadKey();
}
}
}
运行效果截图
以上就是全部代码和说明,对于很多学过Java和C#的小伙伴来说,也很容易理解。由于本人用Java连接SQL Server数据库一直失败,所以就暂时用这种方法来实现;而C#连接SQL数据库的程序代码和窗体登录程序之前我已经写过了,感兴趣的可以去看看。实现账号注册的关键就在于能否找到一个存储途径,剩下的功能代码都是大同小异。除了通过列表来实现,还可以通过文件存储、连接数据库来实现。