This article shares the specific code of the Java lottery system for your reference. The specific content is as follows
User information category
/* * User information class* 1. Account number* 2. Password* 3. Card number* 4. Whether to log in*/ public class User { public static String userName = ""; public static String passwordWord = ""; public static int cardNumber = 0; // Whether to log in public static boolean isLogin = false; // Whether to register public static boolean isRegister = false; }Registration Class
/* * Registration class* 1. Enter the account password* 2. Generate a random number [1000,2000] * 3. Save user registration information* 4. Prompt registration successfully*/ public class Register { // Registration method public static void userRegister() { System.out.println("Please enter username:"); Scanner scanner = new Scanner(System.in); // Receive user information String userName = scanner.nextLine(); System.out.println("Please enter password:"); String password = scanner.nextLine(); int num = (int)(Math.random() * 1001 + 1000); // Save to user class User.userName = userName; User.passWord = password; User.cardNumber = num; // Print registration information System.out.println(); System.out.println("Register successfully, please remember your membership card number"); System.out.println("Username:" + userName); System.out.println("Password:" + password); System.out.println("Card number:" + num); // Save the registration status User.isRegister = true; } }Login class
/* * Login class* 1. Enter the login account and password* 2. Match with user information* There are three chances of re-entering* 3. Login successfully*/ public class Login { // Save the number of times login failed static int num = 0; // Login method// The static member variable needs to be used in the static method public static boolean userLogin() { // First determine whether to register if (User.isRegister == false) { System.out.println("Please register first"); Register.userRegister(); // Login failed return false; } // Enter information System.out.println("User name:"); Scanner scanner = new Scanner(System.in); // Receive information String userName = scanner.nextLine(); System.out.println("Password:"); String password = scanner.nextLine(); // Determine matching login information if(userName.equals(User.userName) && password.equals(User.passWord)) { // Login successfully System.out.println("Welcome" + userName); // Return login result return true; } else { // Login failed num++; // Prompt the user how many chances are left System.out.println("The username or password is incorrect, please re-enter!" + "Left" + (3 - num) + "times"); // Determine how many times the login is wrong if (num != 3) { // Continue to log in userLogin(); } else { // Login failed System.out.println("Sorry for using up three chances, please come again tomorrow!"); // Reset the variable that records the number of logins num = 0; } // If the code goes here, it must be login failed and return false; } } } }Lottery category
/* * Lucky Lottery Class* 1. Determine whether to log in* 2. Enter the Lucky Lottery Card Number* There are three chances to enter the card number* 3. Determine whether to win*/ public class CJ { // Save the number of times you enter the card number static int cardNumber = 0; // Lottery method public static void userCJ() { // Determine the login status if (!User.isLogin) { // End the method System.out.println("Please log in first"); return; } // Determine whether to enter correctly if(!isCarNum()) { System.out.println("You entered incorrectly"); return; } // Match the account// Save the variable to win whether the winning is boolean isCJ = false; // Random five numbers and splice them into strings and print commas to separate String string = "Today's lucky number:"; for (int i = 0; i < 5; i++) { // Random int num = (int)(Math.random() * 1001 + 1000); // Splice if (i < 4) { string = string + num + ","; } else { string = string + num; } // Check if if (num == User.cardNumber) { isCJ = true; } } // Print the winning number System.out.println(string); // Determine whether to win if(isCJ) { System.out.println("winning"); } else { System.out.println("not won"); } } // Enter the card number method public static boolean isCarNum() { System.out.println("Please enter the card number:"); Scanner scanner = new Scanner(System.in); // Receive String carNum = scanner.nextLine(); // Turn int int num = Integer.parseInt(carNum); // Compare if (User.cardNumber == num) { // Match correctly System.out.println("Card number input correct"); return true; } else { // Match incorrect cardNumber++; System.out.println("Input left" + (3 - cardNumber) + "times"); if (cardNumber != 3) { isCarNum(); } else { System.out.println("3 chances to be used up"); // Enter the number of card numbers and reset cardNumber = 0; } return false; } } }Lucky lottery category (assembly lottery logic)
/* * Lottery start class (assembly lottery logic) * */ public class CJStart { // Lottery start public void cjStart() { // Whether to continue the main menu String isGoOn = ""; // Loop main menu do { System.out.println("******Welcome to the lottery system*******"); System.out.println(" 1. Register"); System.out.println(" 2. Login"); System.out.println(" 3. Lottery"); System.out.println("******************************"); Scanner scanner = new Scanner(System.in); // Receive menu option String menuNum = scanner.nextLine(); // Determine option chooseMenuNum(menuNum); // Whether to receive System.out.println("Does continue to select? y/n"); isGoOn = scanner.nextLine(); } while (isGoOn.equals("y")); } // Determine menu option public void chooseMenuNum(String menuNum) { switch (menuNum) { case "1": System.out.println("[Blue European Lottery System> Registration]"); System.out.println("Please fill in personal registration information:"); // Call the registration method Register.userRegister(); break; case "2": System.out.println("[Blue European Lottery System> Login]"); // Call the login method to save the login status boolean userLogin = Login.userLogin(); User.isLogin = userLogin; break; case "3": System.out.println("[Blue European Lottery System> Lucky System]"); // Call the lottery method CJ.userCJ(); break; default: System.out.println("Input error, please reselect"); break; } } }Test class
/* * Test class*/ public class CJTest { public static void main(String[] args) { CJStart cjStart = new CJStart(); cjStart.cjStart(); } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.