Scanner, Random and ArrayList

1、Scanner

Introduction: The Scanner class was introduced in the jdk1.5 version. It is under the java util toolkit and is mainly used to scan the text entered by the user from the console. When we need to input data through the console, we only need to import the Scanner class in the java.util package in advance, and then call the Scanner class, our program can get the data we input in the console.

 Instructions:

Create a Scanner object

Scanner  sc = new  Scanner(System.in);

Create a variable to receive the data

//根据数据类型的不同使用对应的方法接收数据,方法如下:
int a = sc.nextInt();
double b = sc.nextDouble();
byte c = sc.nextByte();
short d = sc.nextShort();
long e = sc.nextLong();
boolean f = sc.hasNextBoolean();
String g = sc.next();
String h = sc.nextLine();

Example:

 public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数字");
        int a = sc.nextInt();
        System.out.println("您输入的数字是:"+a);
    }

 Summary of Scanner methods:

 2、Random

Function: generate a random number

Instructions:

Create a Random object

Random random = new Random();

Create a variable to receive the random number

//由于随机数生成范围较大,所以我们要限制随机数生成的范围
//括号限制随机数范围0--n(包括0不包括n)
int num = random.nextInt(100);

Example:

public static void main(String[] args) {
        Random random = new Random();
        int a = random.nextInt(10);
        System.out.println(a);
    }

3、ArrayList 

concept:

The ArrayList class is an array that can be dynamically modified. The difference from ordinary arrays is that it has no fixed size limit, and we can add or delete elements. ArrayList inherits from AbstractList and implements List interface.

usage:

ArrayList<(data type)> (object name) =new ArrayList<>();

add() adds an element

remove() deletes an element

size() calculates the length

 add element

public static void main(String[] args) {
        ArrayList<String> name = new ArrayList<>();
        name.add("蔡徐坤");
        name.add("练习生");
        name.add("两年半");
        name.add("唱、跳、rap、篮球");
        for (int i = 0;i<name.size();i++){
            System.out.println(name.get(i));
        }
    }

operation result:

delete element

Use the remove() method

Format:

Object name.remove (the subscript of the element to be deleted)

public static void main(String[] args) {
        ArrayList<String> name = new ArrayList<>();
        name.add("蔡徐坤");
        name.add("练习生");
        name.add("两年半");
        name.add("唱、跳、rap、篮球");
        name.add("小黑子");
        for (int i = 0;i<name.size();i++){
            System.out.println(name.get(i));
        }
        System.out.println("-----------删除后------------");
        name.remove(4);
        for (int i = 0;i<name.size();i++){
            System.out.println(name.get(i));
        }
    }

result:

 Example:

Use Scanner and Random to complete the number guessing game. When the input number is greater than the generated random number, it will prompt that the guess is too high.

public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        //限制随机数范围为0--100
        int num = random.nextInt(100)+1;
        //定义一个变量来记录一共猜了几次
        int c = 1;
        do {
            System.out.println("请输入一个数字0--100");
            int a = scanner.nextInt();
            if (a>num){
                System.out.println("猜大了");
            }else if (a<num){
                System.out.println("猜小了");
            }else{
                System.out.println("猜对了");
                猜对后使用break结束循环
                break;
            }
            c++;
        }while (true);
        System.out.println("一共猜了"+c+"次");
    }

 

Complete a non-repetitive random roll call system:

public static void main(String[] args) {
        //实例化对象并添加数据
        List<String> list = Arrays.asList("张三","李四","王五","赵六","田七");
        ArrayList<String> names = new ArrayList<>(list);
        Scanner sc = new Scanner(System.in);
        Random random = new Random();
        //使用while循环完成连续点名操作
        while (true){
            System.out.println("输入Y/y开始点名");
            String a = sc.next();
            if (a.equals("Y")|| a.equals("y")){
                //生成随机数并点名
                int b = random.nextInt(names.size());
                String c = names.get(b);
                System.out.println(c);
                //删除被点中的幸运儿
                names.remove(b);
                //当集合中没有元素时结束循环
                if (names.size() == 0){
                    System.out.println("点名结束");
                    break;
                }
            }else {
                //当输入值不是Y/y时结束循环
                System.out.println("点名结束");
                break;
            }
        }
    }

Guess you like

Origin blog.csdn.net/weixin_69420643/article/details/128026301