The basic use of Scanner Random ArrayList and complete a non-repetitive roll call program

1. Introduction to the Scanner class

Introduction:

The Scanner class is a class in the java.util package

effect:

Commonly used for console input, this class can be called when console input is required

2. Usage of Scanner class

Instructions:

1. First, you need to construct an object of the Scanner class

Scanner sc=new Scanner(System.in);

2. Call the method of the Scanner class object sc to get the input you need from the input stream

illustrate:

When a Scanner class object is created, the console will wait for input until the Enter key is pressed, and the input content is passed to the object sc. To obtain the required content, just call the method of sc

3. The concept of Random

Instances of this class are mainly used to generate random numbers

1. Generation of Random objects

The Random class contains two construction methods, which are introduced in turn below:

1.public Random()

This construction method uses a number related to the relative time corresponding to the current system time as a seed number, and then uses this seed number to construct a Random object.

2.public Random(long seed)

This constructor can be created by specifying a seed number

Sample code:

Random r = new Random();

Random r1 = new Random(10);

2. Basic usage in the Random class

1.random.random()

Randomly generate floating point numbers between (0,1)

2.random.randint (upper limit, lower limit)

Randomly generate an integer within the range, and the two parameters represent the upper and lower limits respectively

3.random.randrange( , , )

Within the specified range, a random number is obtained from the set incremented by the specified base. There are three parameters. The first two parameters represent the upper and lower limits of the range, and the third parameter is the incremental increment, excluding the lower limit and including the upper limit

The usage is as follows:

random.randrange( 0,11,2)。

Randomly generate numbers in the range of 10 with an interval of 2.

Note: one of (0,2,4,6,8,10) is output here

4.random.choice(list)

Randomly select a number from a sequence

5.random.shuffle(list)\nrandom sorting

Code Example: Guess the Number Game

public static void main(String[] args) {
        //使用Scanner和Random完成一个猜数字的小游戏,随机生成一个人1-100之间的数字,
        //然后用户猜数字,大了提示数字猜大了,小了提示数字猜小了,猜对则统计猜的次数
        Scanner sc=new Scanner(System.in);
        Random random=new Random();
        int num=random.nextInt(100)+1; //生成0-99
        //统计猜对的次数
        int count=1;
        //使用死循环进行猜数字,直到猜对结束循环
        do {
            //用户输入的数字
            System.out.println("请输入你的数字:");
            int input= sc.nextInt();
            //判断猜的大小
            if(input>num){
                System.out.println("数字猜大了!");
            } else if (input<num) {
                System.out.println("数字猜小了!");
            }else {
                System.out.println("恭喜你猜对了!");
                break;
            }
           count++;
        }while (true);
        System.out.println("本次游戏共猜了:"+count+"次");
        //关闭资源
        sc.close();
    }

Four. The concept of ArrayList

The ArrayList class is a dynamic array type, and an element can be added or removed from the linked list at any time. ArrayList implements the List interface and can change size dynamically. When we don't know the number of data elements, we can use ArrayList; if we know the number of data elements, we can use array directly.

1. Basic usage of the ArrayList class

1. Add elements

 ArrayList <Integer> list = new ArrayList<>();
  list.add(1);                       //添加1
  

  for (int i = 0; i <= 10; i++) {
           list.add(i);              //添加0至10
 }

2. Delete elements

list.remove(0);   //删除索引为0的元素

3. Get elements

int a=list.get(0);   //获取索引为0的元素

4. Collection size

int b=list.size();

5. Replace elements

list.set(1, "10");          // 设置第2个元素为10

6. Traversal method

for (int i = 0; i < list.size(); i++) {
     System.out.println(list.get(i));
}

5. Complete the non-repetitive roll call procedure

public class text01 {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("1","2","3","4","5","6");
        ArrayList<String> names= new ArrayList<>(list);
        Random random= new Random();
        Scanner sc= new Scanner(System.in);

        while (true){
            if (names.size()==0){
                System.out.println("本次点名结束,请重新开始");
            }
            System.out.println("点名请输入Y/y");
            String input=sc.next();
            if (input.equals("Y")||input.equals("y")){
                int index=random.nextInt(names.size());
                String name=names.get(index);
                System.out.println(name+"---天选之人");
                names.remove(index);
            }else {
                System.out.println("点名抽查,欢迎下次使用");
                break;
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/AMYCX/article/details/128065869