Random Name Caller Case

Posted by Stinger51 on Fri, 04 Feb 2022 18:37:48 +0100

Case description:

Random Name Caller, that is, a classmate's name is printed out randomly in the whole class.
To make a random roll caller, it has the following three contents:
Store names of all students
Print the name of the whole class
Randomly name one of them and print to the console

Case Study:

Print out a student's name randomly in the whole class.
We analyze this case and get the following results:
1. Store the whole class name
2. Print the names of everyone in the class
3. Within the total number of classes, a random number is generated randomly to find the name of the classmate corresponding to the random number.
The case must contain the following three elements:
Store names of all students
Overview of Class Name
Randomly name one of them and print to the control desktop
Random rollers are explicitly divided into three functions. If you write code for multiple independent functions together, the code is relatively lengthy, and we can encapsulate them in a single method for different functions, separating the fully independent functions.
When storing the names of students, if each student defines a variable for name storage, there will be too many isolated variables, it is difficult to hold all the data at once. At this point, we can use arrays to solve the storage problem of multiple data.

Detailed process:

1. Store the names of all the students, create an array to store the names of many students, enter each student's name on the keyboard, and store it in the array

	public static void addStudentName(String[] student) {
		//Keyboard input multiple student names stored in container
		Scanner sc = new Scanner(System.in);
		for (int i = 0; i < student.length; i++) {
			System.out.println("Storage section"+i+1+"Name:");
			student[i] = sc.next();
		}
	}

2. Print the name of the whole class

public static void printStudentName(String[] student) {
		//Traverse the array to get each student's name
		for (int i = 0; i < student.length; i++) {
			String name = student[i];
			//Print Class Name
			System.out.println("No."+i+"Student Name:" + name);
		}
	}

3. Get the names of the students you randomly click on

public static String randomStudentName(String[] student) {
		//Get a random index based on the length of the array
		int index = new Random().nextInt(student.length);
		//Get names from arrays by random index
		String name = student[index];
		//Returns the name of the random point to
		return name;
	}

Design of 4.min Function

public static void main(String[] args) {
		System.out.println("--------Random Name Caller--------");
		// Create a container (array) to store the names of multiple classmates
		String[] student = new String[3];
		/*
		 * 1.Store Class Names
		 */
		addStudentName(student);
		/*
		 * 2.Print Class Name
		 */
		printStudentName(student);
		/*
		 * 3.Get the name of the student randomly named and print it
		 */
		String randomName = randomStudentName(student);
		System.out.println("please"+randomName+"Students Answer Questions");
	}

Complete code and results:

import java.util.Random;
import java.util.Scanner;

public class test {
    public static void addStudentName(String[] student) {
        //Keyboard input multiple student names stored in container
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < student.length; i++) {
            System.out.println("Storage section"+i+"Name:");
            student[i] = sc.next();
        }
    }
    public static void printStudentName(String[] student) {
        //Traverse the array to get each student's name
        for (int i = 0; i < student.length; i++) {
            String name = student[i];
            //Print Class Name
            System.out.println("No."+i+"Student Name:" + name);
        }
    }
    public static String randomStudentName(String[] student) {
        //Get a random index based on the length of the array
        int index = new Random().nextInt(student.length);
        //Get names from arrays by random index
        String name = student[index];
        //Returns the name of the random point to
        return name;
    }
    public static void main(String[] args) {
        System.out.println("--------Random Name Caller--------");
        // Create a container (array) to store the names of multiple classmates
        String[] student = new String[3];
        /*
         * 1.Store Class Names
         */
        addStudentName(student);
        /*
         * 2.Print Class Name
         */
        printStudentName(student);
        /*
         * 3.Get the name of the student randomly named and print it
         */
        String randomName = randomStudentName(student);
        System.out.println("please"+randomName+"Students Answer Questions");
    }
}

Result:

--------Random Name Caller--------
Store the 0th name:
Wang Xiaoming 
Store the first name:
Liu Xiaohong
 Store the second name:
Zhang Damin
 No. 0 Student Name: Wang Xiaoming
 First Student Name: Liu Xiaohong
 Second Student Name: Zhang Damin
 Ask Liu Xiaohong to answer the question

The process has ended,Exit code 0

Topics: Java Eclipse IntelliJ IDEA