模拟用户登录场景

要求:

1、给用户三次机会

2、如果用户在三次前登录成功,不需要在输入

3、如果在三次中登录成功,显示登录成功,如果登录失败显示登录失败
 4、如果在三次登录中都没有成功,显示账号被锁
 5、登录成功的账号为"admin",密码为:"jiushimima"

 方法一:

        代码中使用for循环,switch选择语句,if语句,最主要还有标记TAG

import java.util.Scanner;
public class Demo9 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String id = "admin";
		String password = "jiushimima";
		System.out.println("温馨提示:您只有三次机会哦!");
        
        //TAG:做了个标记 然后break 之后可以跳转到标记处
		TAG: for (int i = 0; i < 3; i++) {
			System.out.println("请输入账号:");
			String str1 = scan.next();
			System.out.println("请输入密码:");
			String str2 = scan.next();

            //在switch中嵌套使用if语句
			switch (i) {
			case 0:
				if (str1.equals(id) && str2.equals(password)) {
					System.out.println("登录成功!");
					break TAG;
				} else {
					System.out.println("登录失败,请重新输入!");
				}
				break;
			case 1:
				if (str1.equals(id) && str2.equals(password)) {
					System.out.println("登录成功!");
					break TAG;
				} else {
					System.out.println("登录失败,请重新输入!!");
				}
				break;
			case 2:
				if (str1.equals(id) && str2.equals(password)) {
					System.out.println("登录成功!");
				} else {
					System.out.println("登录失败,账号已被封锁,明天再来!!");
				}
				break;
			}
		}
    }
}

 方法二:

        代码中仅仅使用for循环,if语句

import java.util.Scanner;
public class Demo9 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String id = "admin";
		String password = "jiushimima";
		System.out.println("温馨提示:您只有三次机会哦!");

		for (int i = 0; i < 3; i++) {
			System.out.println("请输入账号:");
			String str1 = scan.next();
			System.out.println("请输入密码:");
			String str2 = scan.next();
			if (i == 0) {
				if (str1.equals(id) && str2.equals(password)) {
					System.out.println("登录成功!");
					break ;
				} else {
					System.out.println("登录失败,请重新输入!");
				}
			}else if (i == 1) {
				if (str1.equals(id) && str2.equals(password)) {
					System.out.println("登录成功!");
					break;
				} else {
					System.out.println("登录失败,请重新输入!!");
				}
			}else {
				if (str1.equals(id) && str2.equals(password)) {
					System.out.println("登录成功!");
				} else {
					System.out.println("登录失败,账号已被封锁,明天再来!!");
				}
			}	
		}	
	}
}

结果:

 

 

 

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值