博客
关于我
验证登陆信息的合法性
阅读量:618 次
发布时间:2019-03-13

本文共 1189 字,大约阅读时间需要 3 分钟。

登录模块的验证逻辑设计是软件开发中常见但重要的一部分。通常情况下,系统需要接收用户输入的身份验证信息并进行判断,这些信息包括用户名和密码。通过if-else语句,可以实现多条件的验证逻辑,确保系统能够正确判断用户身份。

在本实例中,我们创建了一个CheckLogin类来处理登录验证。该类的主要功能是接收用户输入的用户名和密码,并通过条件判断的方式进行验证。具体来说,程序首先检查用户名是否为"mr",如果不符合条件,则会提示用户名非法。接着,程序会检查密码是否为"mrsoft",如果不符合条件,则会提示登录密码错误。如果两个条件都满足,则表示用户登录信息通过验证。

在编写代码时,需要注意字符串的比较方式。字符串是对象,而不是基本数据类型,因此不能使用"=="来比较两个字符串是否相同。正确的方式是使用equals方法来判断两个字符串是否内容相等。这种方法能够确保比较的是字符串的实际内容,而不是它们在内存中的地址。

以下是CheckLogin类的主要代码:

public class CheckLogin {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入登录用户名:");
String username = scan.nextLine();
System.out.println("请输入登录密码:");
String password = scan.nextLine();
if (!username.equals("mr")) {
System.out.println("用户名非法。");
} else if (!password.equals("mrsoft")) {
System.out.println("登录密码错误。");
} else {
System.out.println("恭喜您,登录信息通过验证。");
}
}
}

这个代码实现了一个简单的登录验证功能。通过if-else条件语句,程序能够根据用户输入的信息进行多条件判断,并给出相应的提示信息。如果用户输入的用户名和密码都符合预期,程序就会输出"恭喜您,登录信息通过验证"的提示。

心法领悟:在进行字符串比较时,记住使用equals方法。字符串是对象,不能使用"=="进行比较,否则会比较的是两个字符串对象的内存地址,而不是实际内容。正确的做法是使用equals方法来判断两个字符串是否内容相同。

转载地址:http://mmbaz.baihongyu.com/

你可能感兴趣的文章
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>