Home Java Javagetting Started Example explanation of random class and scanner class

Example explanation of random class and scanner class

Jan 17, 2020 pm 05:04 PM
random scanner Example kind explain

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);
        }
    }
}
Copy after login

(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);
    }
}
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to read multi-line text input using Scanner.nextLine() method in Java? How to read multi-line text input using Scanner.nextLine() method in Java? Nov 18, 2023 pm 02:53 PM

In Java, Scanner is a very useful class that is particularly convenient when reading user input. When reading a single line of text input, we can use the Scanner.nextLine() method. But what do we do when we need to read multiple lines of text input? This article will introduce you to how to use the Scanner.nextLine() method in Java to read multi-line text input and provide specific code examples. let's start! First, we need to understand some basic knowledge

Naming conventions in PHP: How to use camel case naming for classes, methods and variables Naming conventions in PHP: How to use camel case naming for classes, methods and variables Jul 30, 2023 pm 02:43 PM

Naming conventions in PHP: How to use camelCase notation to name classes, methods, and variables In PHP programming, good naming conventions are an important coding practice. It improves code readability and maintainability, and makes teamwork smoother. In this article, we will explore a common naming convention: camelCase and provide some examples of how to use it in PHP to name classes, methods, and variables. 1. What is camel case nomenclature? CamelCase is a common naming convention in which the first letter of each word is capitalized,

PHP error: Unable to declare class repeatedly, solution! PHP error: Unable to declare class repeatedly, solution! Aug 25, 2023 pm 04:13 PM

PHP error: Unable to declare class repeatedly, solution! It is common for developers to encounter problems. In PHP development, we often encounter a common error: the class cannot be declared repeatedly. This problem seems simple, but if not solved in time, the code will not execute correctly. This article will introduce the cause of this problem and provide a solution for your reference. When we define a class in PHP code, if the same class is defined multiple times in the same file or multiple files, an error that the class cannot be declared repeatedly will occur. This is

Use java's Scanner.hasNext() function to determine whether there is another input Use java's Scanner.hasNext() function to determine whether there is another input Jul 24, 2023 am 11:04 AM

Use Java's Scanner.hasNext() function to determine whether there is another input. In Java programming, sometimes we need to read the user's input, but we often cannot determine how many values ​​the user will enter. To solve this problem, Java provides the Scanner class to read the standard input stream. The Scanner class is an important class in the Java standard library. It provides a variety of methods to read different types of values ​​​​in the standard input stream. Among them, hasNext() is Scann

Use Scanner class to implement line-by-line reading and writing operations of Java files Use Scanner class to implement line-by-line reading and writing operations of Java files Dec 28, 2023 pm 12:31 PM

Use the Scanner class to implement line-by-line reading and writing of Java files. In Java programs, we often need to read and write files, and reading and writing files line-by-line is one of the common requirements. When processing large files, reading and writing line by line can improve the efficiency of the program and avoid memory overflow problems. In this article, we will introduce how to use the Scanner class in Java to implement line-by-line reading and writing operations of files, and provide specific code examples. First, we need to create a text file to test

Java generates random double precision floating point numbers using nextDouble() function of Random class Java generates random double precision floating point numbers using nextDouble() function of Random class Jul 25, 2023 am 09:06 AM

Java uses the nextDouble() function of the Random class to generate random double-precision floating-point numbers. The Random class in Java is a pseudo-random number generator that can be used to generate different types of random numbers. Among them, the nextDouble() function is used to generate a random double-precision floating point number. Before using the Random class, we need to import the java.util package first. Next we can create a Random object and use the nextDouble() function

Learn best practice examples of pointer conversion in Golang Learn best practice examples of pointer conversion in Golang Feb 24, 2024 pm 03:51 PM

Golang is a powerful and efficient programming language that can be used to develop various applications and services. In Golang, pointers are a very important concept, which can help us operate data more flexibly and efficiently. Pointer conversion refers to the process of pointer operations between different types. This article will use specific examples to learn the best practices of pointer conversion in Golang. 1. Basic concepts In Golang, each variable has an address, and the address is the location of the variable in memory.

Packaging technology and application in PHP Packaging technology and application in PHP Oct 12, 2023 pm 01:43 PM

Encapsulation technology and application encapsulation in PHP is an important concept in object-oriented programming. It refers to encapsulating data and operations on data together in order to provide a unified access interface to external programs. In PHP, encapsulation can be achieved through access control modifiers and class definitions. This article will introduce encapsulation technology in PHP and its application scenarios, and provide some specific code examples. 1. Encapsulated access control modifiers In PHP, encapsulation is mainly achieved through access control modifiers. PHP provides three access control modifiers,

See all articles