With if, do...while, switch, write a simple supermarket member random lucky member system, including registration, login and lucky draw.

The system is completed as follows:  

  We prepared it step by step:

[step 1] realize the output display of the menu: output the "winner millionaire system" menu, select the menu number and output the menu information. If the number selection is wrong, output "your input is wrong":

First, let's make a typesetting:
        System.out.println("**************************************");
         System.out.println("\t     Welcome to the winner millionaire system ");
         System.out.println("\t\t1. Registration");
         System.out.println("\t\t2. Login");
         System.out.println("\t\t3. Lucky draw");
        System.out.println("**************************************");
         System.out.print("please select menu:");
The first step is to write judgment:
if (A <= 0 || A > 3) {
             System.out.println("input error");
        } else {


The second step is data selection:
You can use if (A == 1){
} else if (A==2){
}... write like this, but too much, and the code looks messy.
Using switch to write will appear very clear, and the code can be understood at a glance.
switch (A) {
            case 1: 
                 System.out.println("winner millionaire system > registration");
            break;
            case 2: 
                 System.out.println("winner millionaire system > login");
            break;
            case 3: 
                 System.out.println("winner rich man system > lucky draw");
            break;
            }
In this way, it is easy to see, but A = 1, 2 and 3 enter the switch, and then output statements in the switch.

[step 2] realize circular execution function: after the user selects a menu and outputs menu information, the system asks whether to continue? If you continue, continue to select the menu, otherwise end the system:

So let's do...while loop this step:
We add do before the overall style to get the following:

import java.util.Scanner;

public class Dome08 {

	public static void main(String[] args) {

		Scanner ning = new Scanner(System.in);
			String B = null ;//Why should this be written outside. Null "store null value" can also write Chen String B = "";
            //Explanation: while(!B.equals("yes") cannot be read when writing.
		do {
            System.out.println("**************************************");
		    System.out.println("\t    Welcome to the winner millionaire system");
		    System.out.println("\t\t1.register");
		    System.out.println("\t\t2.Sign in");
		    System.out.println("\t\t3.luck draw");
		    System.out.println("**************************************");
			System.out.print("Please select menu:");
				int A = ning.nextInt();    //Menu selection
			if (A <= 0 || A > 3) {         //Selected digital judgment
				System.out.println("Input error");
			} else {
				switch (A) {               //Select in switch
				case 1: 
					System.out.println("Winner millionaire system>register");
				break;
				case 2: 
					System.out.println("Winner millionaire system>Sign in");
				break;
				case 3: 
					System.out.println("Winner millionaire system>luck draw");
				break;
				}
				     
			}System.out.print("End:");//Give end statement selection
			       B = ning.next();        //When yes is given, jump out of while, and when no is given, continue the loop   
		} while (!B.equals("yes"));    
		System.out.println("System exit, thank you for using.");
	}

}

[step 3] realize the registration function: the user selects the registration menu to enter the registration interface. The system prompts the user to enter the user name and password, and the system generates a 4-digit random number as the card number. Registration succeeded. The registration information is displayed:

In this step, we need to add code in case 1: 1. According to the category, we conduct random number guided package before the class name, and the guided package range is 1000 ~ 9999. Because this topic is four digits.
2, The defined data types after this step are all written on do, String C = null,D = null;
                     System.out.println("please fill in personal registration information:");
                     System.out.print("user name:");
                        C = ning.next();
                     System.out.print("user password:");
                        D = ning.next();
                     System.out.println("membership card No.: + E);

Then we print and typeset outside the switch, and the demonstration is as follows:
 

import java.util.Scanner;

import java.util.Random;

public class Dome08 {

	public static void main(String[] args) {

		Scanner ning = new Scanner(System.in);
			String B = null ;//Why should this be written outside. Null "store null value" can also write Chen String B = "";
            //Explanation: while(!B.equals("yes") cannot be read when writing.
        Random yun = new Random();
            int E = yun.nextInt(9999)+1000;
		do {
            System.out.println("**************************************");
		    System.out.println("\t    Welcome to the winner millionaire system");
		    System.out.println("\t\t1.register");
		    System.out.println("\t\t2.Sign in");
		    System.out.println("\t\t3.luck draw");
		    System.out.println("**************************************");
			System.out.print("Please select menu:");
				int A = ning.nextInt();    //Menu selection
			if (A <= 0 || A > 3) {         //Selected digital judgment
				System.out.println("Input error");
			} else {
				switch (A) {               //Select in switch
				case 1: 
					System.out.println("Winner millionaire system>register");
				break;
				case 2: 
					System.out.println("Winner millionaire system>Sign in");
				break;
				case 3: 
					System.out.println("Winner millionaire system>luck draw");
				break;
				}
				       
			}
            System.out.println("user name\t User password\t Membership card number");
			System.out.println(C+"\t"+D+"\t"+E);
			System.out.print("End:");//Give end statement selection
				    B = ning.next();        //When yes is given, jump out of while, and when no is given, continue the loop 
		} while (!B.equals("yes"));    
		System.out.println("System exit, thank you for using.");
	}

}

  [step 4] realize the login function: after successful registration, the user selects the "login" menu to enter the login interface. Enter the user name and password at the time of registration. After successful login, the system will prompt welcome information. If the user name and password are entered incorrectly, the user will be prompted to continue to enter. There are up to 3 input opportunities. This step is difficult:

Because the value we defined has not changed after registration. So choose 2. Login directly. Change case 2: directly to if(A==2). Since the user name, user password and membership card number are printed only once, we move these two sentences to case 1: and then use if to judge whether to continue next. To continue means that you chose 2. Log in. Login and input three times, so we also need to define a do... While loop to receive the input user name and password, and judge whether the input is correct. Finally, judge whether it is equal to three times or equal to three times of forced end outside the switch. The demonstration is as follows:

import java.util.Scanner;

import java.util.Random;

public class Dome08 {

	public static void main(String[] args) {

		Scanner ning = new Scanner(System.in);
			String B = null ;//Why should this be written outside. Null "store null value" can also be written as String B = "";
            //Explanation: while(!B.equals("yes") cannot be read when writing.
        Random yun = new Random();
            int E = yun.nextInt(9999)+1000;
                String C = null,D = nullF = null,G = null;
		do {
            System.out.println("**************************************");
		    System.out.println("\t    Welcome to the winner millionaire system");
		    System.out.println("\t\t1.register");
		    System.out.println("\t\t2.Sign in");
		    System.out.println("\t\t3.luck draw");
		    System.out.println("**************************************");
			System.out.print("Please select menu:");
				int A = ning.nextInt();    //Menu selection
			if (A <= 0 || A > 3) {         //Selected digital judgment
				System.out.println("Input error");
			} else {
				switch (A) {               //Select in switch
				case 1: 
					System.out.println("Winner millionaire system>register");
                    System.out.println("Please fill in your personal registration information:");
					System.out.print("user name:");
						C = ning.next();
					System.out.print("User password:");
						D = ning.next();
					System.out.println("Membership card No.:"+E);
                    System.out.println("user name\t User password\t Membership card number");
			        System.out.println(C+"\t"+D+"\t"+E);
			        System.out.print("End:");//Give end statement selection
				        B = ning.next();        //When yes is given, jump out of while, and when no is given, continue the loop 
		            if (!B.equals("yes")) {        //Determine whether to continue
					    System.out.println("**************************************");
					    System.out.println("\t    Welcome to the winner millionaire system");
					    System.out.println("\t\t1.register");
					    System.out.println("\t\t2.Sign in");
					    System.out.println("\t\t3.luck draw");
					    System.out.println("**************************************");
					    System.out.print("Please select menu:");
						    A = ning.nextInt();
					    if (A == 2) {        //Select 2. Login
					          System.out.println("Winner millionaire system>Sign in");
						        	int P = 0;
						    do {//Because of the comparison, the data type is stored at the top		
						        System.out.println("Enter user name:");
						        	F = ning.next();
						        System.out.println("Enter user password:");
					        		G = ning.next();
						        	P++;//Add up until the third time
                               if (!(C.equals(F) && D.equals(G))) {
						        System.out.println("Input error");
						       }
						       } while (!(C.equals(F) && D.equals(G)) && !(P == 3));
					//Judge whether the cycle is over: continue the cycle when C is not equal to F, D is not equal to G and P is not equal to 3  
                                 if (P == 3) {//Judge when P==3 ends
							     break;    //Whether the loop is over or not is always known until the selection is over
							    } else {//Otherwise, login is successful
								System.out.println("Welcome:"+C+"Login succeeded");
							break;
							}
						
					    } else {//In other cases, this is rare
						    System.out.println("System exit, thank you for using.");
				    	break;
					    }
						
			    	}		
        				 
					
				case 3: 
					System.out.println("Winner millionaire system>luck draw");
				break;
				}	
				System.out.print("End:");
					B = ning.next();      
			}
            
		} while (!B.equals("yes"));    
		System.out.println("System exit, thank you for using.");
	}

}

[step 5] realize the lucky draw function: after logging in successfully, select the lucky draw menu with the user name. Enter the membership card number, and the system generates 5 4-digit random numbers as lucky numbers. If the membership card number is one of them, you will become a lucky member of the day:

The fifth step is the simplest. Just add a for loop in case 3: five times to judge whether it is equal to the first random number.

import java.util.Scanner;

import java.util.Random;

public class Dome08 {

	public static void main(String[] args) {

		Scanner ning = new Scanner(System.in);
			String B = null ;//Why should this be written outside. Null "store null value" can also be written as String B = "";
            //Explanation: while(!B.equals("yes") cannot be read when writing.
        Random yun = new Random();
            int E = yun.nextInt(9999)+1000;
            int H = 0;
                String C = null,D = null,F = null,G = null;
		do {
            System.out.println("**************************************");
		    System.out.println("\t    Welcome to the winner millionaire system");
		    System.out.println("\t\t1.register");
		    System.out.println("\t\t2.Sign in");
		    System.out.println("\t\t3.luck draw");
		    System.out.println("**************************************");
			System.out.print("Please select menu:");
				int A = ning.nextInt();    //Menu selection
			if (A <= 0 || A > 3) {         //Selected digital judgment
				System.out.println("Input error");
			} else {
				switch (A) {               //Select in switch
				case 1: 
					System.out.println("Winner millionaire system>register");
                    System.out.println("Please fill in your personal registration information:");
					System.out.print("user name:");
						C = ning.next();
					System.out.print("User password:");
						D = ning.next();
					System.out.println("Membership card No.:"+E);
                    System.out.println("user name\t User password\t Membership card number");
			        System.out.println(C+"\t"+D+"\t"+E);
			        System.out.print("End:");//Give end statement selection
				        B = ning.next();        //When yes is given, jump out of while, and when no is given, continue the loop 
		            if (!B.equals("yes")) {        //Determine whether to continue
					    System.out.println("**************************************");
					    System.out.println("\t    Welcome to the winner millionaire system");
					    System.out.println("\t\t1.register");
					    System.out.println("\t\t2.Sign in");
					    System.out.println("\t\t3.luck draw");
					    System.out.println("**************************************");
					    System.out.print("Please select menu:");
						    A = ning.nextInt();
					    if (A == 2) {        //Select 2. Login
					          System.out.println("Winner millionaire system>Sign in");
						        	int P = 0;
						    do {//Because of the comparison, the data type is stored at the top		
						        System.out.println("Enter user name:");
						        	F = ning.next();
						        System.out.println("Enter user password:");
					        		G = ning.next();
						        	P++;//Add up until the third time
                               if (!(C.equals(F) && D.equals(G))) {
						        System.out.println("Input error");
						       }
						       } while (!(C.equals(F) && D.equals(G)) && !(P == 3));
					//Judge whether the cycle is over: continue the cycle when C is not equal to F, D is not equal to G and P is not equal to 3  
                                 if (P == 3) {//Judge when P==3 ends
							     break;    //Whether the loop is over or not is always known until the selection is over
							    } else {//Otherwise, login is successful
								System.out.println("Welcome:"+C+"Login succeeded");
							break;
							}
						
					    } else {//In other cases, this is rare
						    System.out.println("System exit, thank you for using.");
				    	break;
					    }
						
			    	}
                    break;		
				case 3:     //luck draw
					System.out.println("Winner millionaire system>luck draw");
                for (int i = 1; i <= 5; i++) {
					 H = yun.nextInt(9999)+1000;
					System.out.print(H+"\t");
			    }
					System.out.println();
				if (E == H) {
					System.out.println("Congratulations on being a lucky member");
				} else {
					System.out.println("I'm sorry you're not a lucky member");
				}
				break;
		        }System.out.print("End:");
					B = ning.next();      
			}
            
		} while (!B.equals("yes"));    
		System.out.println("System exit, thank you for using.");
	}

}

 

The final demonstration result is:

 

 

The main ideas are clear and coherent.

Keywords: Java Eclipse

Added by atholon on Thu, 07 Oct 2021 04:48:40 +0300