The teacher wrote a two-color ball system. code show as below:
package com.main;
import java.util.Random;
import java.util.Scanner;
public class SystemTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
//1. Store account, password null
String[][] userArray = new String[100][2];
/*
[["Lily ", "123"],
["Jack", "123"],
]
*/
//2. Store lottery result 0
int[][] dataArray = new int [100][7];
/*
[ [2, 17, 15, 22, 34, 8, 6]
[2, 17, 15, 22, 34, 8, 6]
[2, 17, 15, 22, 34, 8, 6] ]
*/
//3. Definition Input object scanner
Scanner scanner = new Scanner(System.in); //4.Define
bool type variable for loop system
boolean isRun = true; //5.Define variable
for logging login status
boolean isLogin = false;
// Enter lottery system
while (isRun) {
System.out.println("Welcome to the two-color ball system");
System.out.println("1. Registration");
System.out.println("2. Login");
System.out.println(" 3. Machine selection");
System.out.println("4. Optional");
System.out.println("5. View");
System.out.println("6. Prize draw");
System.out. println("7. Exit the system");
System.out.println("Enter your choice:");
int result = scanner.nextInt();
switch (result) {
case 1:
//Register
//1.User The name cannot be repeated
//2. Enter the same password twice
//3. Store user information: account, password
//Store username status
boolean hasName = true;
//Store username
String name = "";
//Console input UsernameSystem.out
.println("Please enter username:");
while(hasName){
name = scanner.next();
//Traverse, judge
for(int i = 0; i < userArray.length; i++){
//If the name is repeated, you cannot register
if(userArray[i][0] != null && userArray[i][0].equals(name) ){
System.out.println("The username already exists, please re-enter:");
hasName = true;
break;
}else {
hasName = false;
}
}
}
/ /Judge the password
//Define the variable to judge whether the password is consistent
boolean isEqual = true;
String pwd = null;
while(isEqual){
System.out.println("Please enter the password:");
pwd = scanner.next();
System. out.println("Please enter the password again:");
String repeat = scanner.next();
//Determine the password
if(pwd.equals(repeat)){
//consistent twice
isEqual = false;
}else {
System.out.println("The passwords entered twice are inconsistent!");
isEqual = true;
}
}
//---------------------------------
//Add user information
for(int i = 0; i < userArray.length; i++){
if(userArray[i][0] == null){
//Add information
userArray[i][0] = name;
userArray[i][1] = pwd;
System.out. println("Successful registration! Awesome!");
break;
}
}
break;
case 2:
//Login
if(isLogin){
System.out.println("Login!");
break;
}
// Not logged in !
System.out.println("Please enter the username:");
String name1 = scanner.next();
System.out.println("Please enter the password:");
String pwd1 = scanner.next();
//遍历判断!!!
for(int i = 0; i < userArray.length; i++){
if(userArray[i][0] != null && userArray[i][0].equals(name1) && userArray[i][1].equals(pwd1)){
System.out.println("登陆成功!");
isLogin = true;
break;
}else{
isLogin = false;
}
}
if(isLogin == false){
System.out.println("登陆失败!");
}
break;
case 3:
//机选
if(isLogin == false){
System.out.println("Please log in!");
break;
}
//Execute here! Description logged in! Start machine selection!
System.out.print("Please enter the number of notes for machine selection:");
int count = scanner.nextInt();
Random random = new Random();
//The number of outer control lines is the number of lottery bets
for(int i = 0; i < count; i++){
int num[] = new int[ 7];
//The inner layer starts the first 6 red balls at random
for(int j = 0; j < 6; j++){
int temp = random.nextInt(33) + 1;
//Define the variable to store whether the ball is repeated
boolean isExist = false;
//This loop is used to iterate through the array!!!!
for(int k = 0; k < 6; k++){
if(temp == num[k]){
isExist = true; j--
;
break;
}
}
if(isExist == false){
//Add
num[j] = temp if the number does not exist;
}
}
//Sort the red balls in ascending order
for(int m = 0; m < 6 - 1; m++){
for(int n = 0; n < 6 - 1 - m; n++){
if(num[n] > num[n+1]){
int t = num[n];
num[n] = num[n + 1];
num[n + 1] = t;
}
}
}
//add the last blue ball
num[6] = random.nextInt(16) + 1;
//--------------------------------------
//View the result
System.out.println( "The machine selection number is:");
for(int m = 0; m < 7; m++){
if(num[m] < 10){
System.out.print("0" + num[m] + " " );
}else{
System.out.print( num[m] + " ");
}
}
System.out.println();
//Put the result into a two-digit array!!!!
for(int j = 0 ; j < dataArray.length; j++){
if(dataArray[j][0] == 0) {
for(int k = 0; k < 7; k++){
dataArray[j][k] = num[k] ;
}
break;
}
}
}
break;
case 4:
//Optional
break;
case 5:
//Check
if(isLogin == false){
System.out.println("Please log in!");
break;
}
//Output two-digit array!! !
for(int i = 0; i < dataArray.length; i++){
if(dataArray[i][0] == 0){
break;
}
//loop through the one-dimensional array
for(int j = 0; j < 7; j++){
if(dataArray[i][j] < 10){
System.out.print("0" + dataArray[i][j] + " ");
}else{
System.out.print( dataArray[i][j] + " ");
}
}
System.out.println();
}
break;
case 6:
//Lottery
break;
case 7:
//Logout
isRun = false;
break;
default:
break;
}
}
}
}