Home  >  Article  >  Java  >  Java uses the nextInt() function of the Scanner class to obtain the integer value input by the console and specify the input base.

Java uses the nextInt() function of the Scanner class to obtain the integer value input by the console and specify the input base.

PHPz
PHPzOriginal
2023-07-25 08:52:561701browse

Java uses the nextInt() function of the Scanner class to obtain the integer value input by the console, and at the same time specify the input base

In Java programming, we often need to obtain data from the console input, and when processing integers When inputting, Java provides the nextInt() function of the Scanner class. This function can get the integer value entered by the console and store it in an integer variable. However, by default, the Scanner class can only read decimal integers. So, what if we need to read integers in other bases from the console? This article will introduce how to use the nextInt() function of the Scanner class to obtain the integer value input by the console and specify the input base.

First, we need to import the Java.util package in order to use the Scanner class. The following is the corresponding code example:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入一个十六进制整数:");
        int hexInt = scanner.nextInt(16);
        System.out.println("输入的整数为:" + hexInt);

        System.out.print("请输入一个八进制整数:");
        int octInt = scanner.nextInt(8);
        System.out.println("输入的整数为:" + octInt);

        System.out.print("请输入一个二进制整数:");
        int binInt = scanner.nextInt(2);
        System.out.println("输入的整数为:" + binInt);
    }
}

In the above code, we first create a Scanner object scanner for reading console input. Then, we use the scanner.nextInt() function to obtain hexadecimal, octal and binary input respectively, and store the input integer value in the corresponding integer variable.

When calling the scanner.nextInt() function, we specify the corresponding base through the parameters. For example, parameter 16 means that we want to get a hexadecimal integer, parameter 8 means that we want to get an octal integer, and parameter 2 means that we want to get a binary integer.

When inputting on the console, we need to enter integers according to the corresponding base. For example, if we want to enter a hexadecimal integer, we need to start with 0x when entering, such as 0x1F; if we want to enter an octal integer, we need to start with 0 when entering, such as 037; if we want to enter an Binary integers, we need to start with 0b when input, for example 0b101.

Finally, we output the obtained integer value to the console through the print statement.

To summarize, by using the nextInt() function of the Scanner class, we can easily obtain the integer value input by the console and specify the input base. This is useful for handling integer input in various bases. Hope this article can be helpful to you.

The above is the detailed content of Java uses the nextInt() function of the Scanner class to obtain the integer value input by the console and specify the input base.. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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