介绍
Java小程序,不多说,代码有注释且很详细
import java.util.Scanner;
/**
* @ClassName UserLuckySystem
* @Date 2023-07-28
* @Time 12:25
* @Version 3.0
*/
public class UserLuckySystem {
public static void main(String[] args) {
//控制台输入流
Scanner input = new Scanner(System.in);
//注册的用户名
String name;
//注册的密码
String password;
//登录的密码
String password2;
//会员号
String ranNum1 = null;
/*存储用户名,密码,会员号
设置注册会员的人数上限为10人
*/
String[][] information = new String[10][3];
//键盘输入1,2,3的选择
String select01;
/*用于控制系统整体循环,键盘输入y/n的选择
temp1 = 1;执行循环
temp1 = 4;跳出循环、退出系统
*/
int temp1 = 1;
//二维数组
int count1 = 0;
int count2 = 0;
//选项3.抽奖,用于表头用户名信息更新
int count3 = 0;
//注册标识符
boolean isSign = false;
//登录标识符
boolean isLogin = false;
/*系统循环
temp1 == 1,执行循环;temp == 0,退出循环;
*/
while (temp1 == 1) {
//控制 case"1"中的while循环
int temp2 = 1;
//控制 case"2"中的用户密码输入,密码输入错误3次即跳出while循环
int flag1 = 0;
System.out.println("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*");
System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t欢迎进入奖客富翁系统");
System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t1.注册");
System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t2.登录");
System.out.println("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t3.抽奖");
System.out.println("*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*\t*");
System.out.print("请选择菜单:");
//键盘输入select01为"1"/"2"/"3"的一种时,进入相应的选项
select01 = input.next();
if ("1".equals(select01) || "2".equals(select01) || "3".equals(select01)) {
switch (select01) {
case "1":
int flag2 = 1;
System.out.println("【奖客富翁系统 > 注册】");
System.out.println("请添加个人注册信息:");
//验证用户名是否存在
do {
System.out.print("用户名:");
name = input.next();
int i;
//遍历数组第一行,匹配用户名,判断是否已经注册
for (i = 0; i < 10; i++) {
if (name.equals(information[i][0])) {
System.out.println("无法注册,该用户名已经存在!");
i = 0;
break;
}
}
if (i != 0) {
flag2 = 0;
}
} while (flag2 == 1);
while (true) {
System.out.print("密码:");
password = input.next();
for (int i = 0; i < 4; i++) {
int a = (int) (1 + Math.random() * 9);
//将int形的会员号转换为String类型的会员号,拼接
ranNum1 += String.valueOf(a);
}
//System.out.println("注册成功!\n请记好您的会员卡号" + ranNum1);
System.out.println("注册成功!\n请记好您的会员卡号!忘记卡号将无法进行3.抽奖功能!");
//二维数组第一行用来存储用户名
information[count1][count2] = name;
System.out.print((information[count1][count2]) + "\t\t");
//二维数组第二行用来存储密码
information[count1][++count2] = password;
System.out.print((information[count1][count2]) + "\t\t");
//二维数组第三行用来存储会员号
//substring:截取字符串,beginIndex为从第几位截取字符串
information[count1][++count2] = ranNum1.substring(4);
System.out.print((information[count1][count2]));
ranNum1 = null;
System.out.println();
//数组下标加一,用来存放下一位注册会员信息
count1++;
count2 = 0;
System.out.println("是否继续(y/n)");
char answerString = input.next().charAt(0);
if (answerString == 'y' || answerString == 'Y') {
//执行switch外的循环
break;
} else {
//终止switch外的循环
temp1 = 0;
break;
}
}
break;
case "2":
System.out.println("【奖客富翁系统 > 登录】");
System.out.print(" 请输入用户名:");
String username2 = input.next();
//遍历information数组,与控制台输入的用户名一一比较
for (int i = 0; i < 10; i++) {
flag1++;
//判断是否存在此用户
if (username2.equals(information[i][0])) {
while (temp2 < 4) {
System.out.print("请输入密码:");
password2 = input.next();
//判断登录密码是否正确
if (password2.equals(information[i][1])) {
System.out.println("欢迎您\t\t" + information[i][0]);
ranNum1 = information[i][2];
count3 = i;
temp2 = 4;
} else {
//密码输入错误三次,退出系统
System.out.println("密码输入错误,还剩" + (3 - temp2) + "次输入机会!");
temp2++;
if (temp2 == 4) {
temp1 = 0;
break;
}
}
}
}
//输入错误密码达到上限次数
if (temp2 == 4) {
break;
}
//数组用户名循环比较
if (flag1 == 10) {
System.out.println("不存在此用户!");
System.out.println("是否继续(y/n)");
char answerString = input.next().charAt(0);
if (answerString == 'y' || answerString == 'Y') {
//执行switch外的循环
break;
} else {
//终止switch外的循环
temp1 = 0;
break;
}
}
}
break;
case "3":
if (ranNum1 == null) {
System.out.println("请先登录!");
continue;
}
System.out.println("【" + information[count3][0] + "奖客富翁系统 > 抽奖】");
String cardId;
System.out.print("请输入您的卡号:");
do {
cardId = input.next();
if(!ranNum1.equals(cardId)){
System.out.print("输入错误,请重新输入:");
}else {
break;
}
}while (true);
System.out.print("本日幸运数字为:");
//定义长度为5的数组,用来存放5组四位幸运数字
String[] luckNums = new String[5];
String luckNumber = null;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
//随机生成4个随机数字
int a = (int) (1 + Math.random() * 9);
//数字转化为字符串,拼接
luckNumber += String.valueOf(a);
}
//字符串截取
luckNums[i] = luckNumber.substring(4);
luckNumber = null;
System.out.print(luckNums[i] + "\t");
}
System.out.println();
for (int i = 0; i < 5; i++) {
if (ranNum1.equals(luckNums[i])) {
System.out.println("恭喜!您是本日的幸运会员!");
}
}
System.out.println("抱歉!您不是本日的幸运会员!");
System.out.println("是否继续(y/n)");
char answerString2 = input.next().charAt(0);
if (answerString2 == 'y' || answerString2 == 'Y') {
//执行switch外的循环
break;
} else {
//终止switch外的循环
temp1 = 0;
break;
}
default:
System.out.println("您的输入有误!");
break;
}
} else {
//键盘输入select01不为"1"/"2"/"3"的一种时,提示输入正确的数
System.out.println("请输入正确的序号!!!\n\n\n");
}
}
System.out.println("退出系统,谢谢使用");
System.out.println("注册信息如下:");
System.out.println("用户名\t\t\t\t\t\t\t\t\t\t密码\t\t\t\t\t\t\t\t\t\t会员号");
//测试用,二重循环输出当前用于存储用户信息的数组
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(information[i][j] + "\t\t\t\t\t\t\t\t\t\t");
}
System.out.println();
}
}
}
演示
- 注册
- 登录
- 抽奖
参考