任务:模拟注册登录幸运抽奖全过程
主要功能:注册、登录、幸运抽奖
阶段1:练习——实现菜单的输出显示
需求说明:输出菜单、选择菜单编号,输出菜单信息 如果编号选择错误,输出“您的输入有误!”
阶段2:练习——实现循环执行功能
需求说明:系统询问用户是否继续,如果用户选择继续,则可以继续选择菜单,否则程序结束,退出系统
阶段3:练习——实现注册功能
需求说明:输入用户名和密码,系统产生4位随机数作为卡号。 注册成功,显示注册信息并修改注册标识为true
阶段4:练习——实现登录功能
需求说明:输入注册时的用户名和密码,登录成功,系统提示欢迎信息;如果用户名和密码输入错误,提示用户继续输入,最多有3次输入机会。
阶段5:练习——实现幸运抽奖功能
需求说明:登录成功后,用户选择幸运抽奖菜单,进入幸运抽奖功能 输入会员卡号,系统生成5个4位随机数作为幸运数字;如果会员卡号是其中之一,则成为本日幸运会员;否则不是幸运会员。
import java.util.Random;
import java.util.Scanner;
import com.etc.test.util.JudgeNumber;
import com.etc.test.util.RandomNumber;
public class Lucky {
public static void main(String[] args) {
int number = 0;
int card = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
String name = "";
String password = "";
String name2 = "";
String password2 = "";
RandomNumber ra = new RandomNumber();
JudgeNumber jd = new JudgeNumber();
b = ra.randomfour();
c = ra.randomfour();
d = ra.randomfour();
e = ra.randomfour();
f = ra.randomfour();
while (true) {
System.out.println("*****欢迎进入奖课富翁系统*****");
System.out.println("\t1.注册");
System.out.println("\t2.登陆");
System.out.println("\t3.抽奖");
System.out.println("***************************");
System.out.print("请选择菜单:");
number = jd.judge();
switch (number) {
case 1:
int a = 0;
System.out.println("[奖客富翁系统> 注册]");
System.out.println("请填写个人信息:");
name = jd.name();
System.out.println("密码");
password = jd.name();
a = ra.randomfour();
System.out.println("注册成功,请记好您的会员卡号");
System.out.println("用户名\t密码\t会员卡号");
System.out.println(name + "\t" + password + "\t" + a);
break;
case 2:
System.out.println("奖客富翁系统> 登陆");
for (int k = 1; k <= 3; k++) {
System.out.println("请输入用户名");
name2 = jd.name();
System.out.println("请输入密码:");
password2 = jd.name();
if (name2.equals(name) && password2.equals(password)) {
System.out.println("欢迎您" + name2);
break;
} else if ((3 - k) == 0) {
System.out.println("您输入的3次用户名或密码均有误,系统退出!");
continue;
} else {
System.out.println("您输入的用户名或密码有误,您还有" + (3 - k) + "次机会");
}
}
break;
case 3:
System.out.println("[奖客富翁系统> 抽奖]");
System.out.println("请输入您的卡号:");
card = jd.judge();
if (card == b || card == c || card == d || card == e || card == f) {
System.out.println("本日数字为:\t" + b + "\t" + c + "\t" + d + "\t" + e + "\t" + f);
System.out.println("恭喜您成为本日幸运会员");
} else {
System.out.println("本日数字为:\t" + b + "\t" + c + "\t" + d + "\t" + e + "\t" + f);
System.out.println("抱歉!您不是本日的幸运会员!");
}
break;
default:
System.out.println("您输入的有误!");
break;
}
System.out.println("继续吗?(y/n):");
String number2 = jd.name();
if (number2.equals("n")) {
break;
}
}
}
}
public class RandomNumber {
public int randomone() {
int number = 0;
return number=(int) ((Math.random() * 9 + 1) );
}
public int randomtwo() {
int number = 0;
return number=(int) ((Math.random() * 9 + 1) * 10);
}
public int randomthree() {
int number = 0;
return number=(int) ((Math.random() * 9 + 1) * 100);
}
public int randomfour() {
int number = 0;
return number=(int) ((Math.random() * 9 + 1) * 1000);
}
}
import java.text.SimpleDateFormat;
import java.util.Scanner;
import java.util.Date;
public class JudgeNumber {
public int judge() { // 判断int类型
int number = 0;
while (true) {
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
number = input.nextInt();
break;
} else {
System.out.print("请输入数字:");
}
}
return number;
}
public int odd() { // 判断输入的数字是否为奇数
int number = 0;
while (true) {
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
number = input.nextInt();
} else {
System.out.print("请输入数字:");
}
if (number % 2 == 1) {
break;
} else {
System.out.print("请输入奇数:");
}
}
return number;
}
public String name() { // 判断String类型
String name = "";
while (true) {
Scanner input = new Scanner(System.in);
if (input.hasNext()) {
name = input.next();
break;
}
}
return name;
}
public int month() { // 判断月份格式和范围
int number = 0;
while (true) {
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
number = input.nextInt();
} else {
System.out.print("请输入数字:");
}
if (0 < number && number <= 12) {
break;
} else {
System.out.print("请输入正确数字:");
}
}
return number;
}
public int day() { // 判断日期格式和范围
int number = 0;
while (true) {
Scanner input = new Scanner(System.in);
if (input.hasNextInt()) {
number = input.nextInt();
} else {
System.out.print("请输入数字:");
}
if (0 < number && number <= 31) {
break;
} else {
System.out.print("请输入正确数字:");
}
}
return number;
}
public String Date() { // 判断年月日格式
String time="";
while (true) {
SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd");
Scanner input = new Scanner(System.in);
time = input.next();
try {
Date data= sd.parse(time);
break;
} catch (Exception e) {
System.out.println("请输入正确格式:");
}
}
return time;
}
}