search
HomeJavaJavagetting StartedExample explanation of random class and scanner class

Example explanation of random class and scanner class

Jan 17, 2020 pm 05:04 PM
randomscannerExamplekindexplain

Example explanation of random class and scanner class

Generate a random number within the specified range

Math.random() Generate a random number, the random number is 0 Between 1 and 1, the type is double.

Code example:

public class randCase {
    public static void main(String[] args) {
        double rand = 0;
        for (int i = 0; i < 10; i++) {
            rand = Math.random();
            System.out.println(rand);
        }
    }
}

(Free learning video tutorial sharing: java video tutorial)

Read the string from the standard input and Integer (gets user input)

Scanner in = new Scanner(System.in) Connect to standard input.

where in represents a variable.

in.nextLine() can read a line of string from the command line.

in.nextInt() can read a positive integer from the command line.

The dot operator is an operator in Java. It is the same operator as the dot in System.out.printf() and Math.random(). It is used to dot the "variable" in front of the dot. "operation". The so-called "operation" refers to the method, which is the main method we have been writing. These operations are performed one by one. The method of use is called invoke a method.

import java.util.Scanner; tells the program where the Scanner type is.

Create a "variable" of Scanner type, its function is to help us read data from the standard input.

Code example:

import java.util.Scanner; // 告诉程序Scanner类型在哪

public class scannerCase {
    public static void main(String[] args) {
        // Scanner.nextLine(); 从命令行中读取一行字符串。
        Scanner in = new Scanner(System.in);
        System.out.println("请输入一句话:");
        String str = in.nextLine();
        System.out.println(str);

        // Scanner.nextInt(); 从命令行中读取一行正整数。
        System.out.println("请输入一个数字:");
        int num = in.nextInt();
        System.out.println(num);
    }
}

The output result is as shown in the figure:

Example explanation of random class and scanner class

Recommended related articles and tutorials: java Quick Start

The above is the detailed content of Example explanation of random class and scanner class. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools