Home  >  Article  >  Java  >  What is the java input statement?

What is the java input statement?

little bottle
little bottleOriginal
2019-05-21 16:48:2022560browse

Java has many statements, among which input statements are one of the most basic operations. Below I will take you through how to write input code.

What is the java input statement?

First of all, before you perform the input operation, you must add the following two packages to the package queue of the Java program.

First import java.io.*; and java.util.*; into the Java code.

import java.util.*;
import java.io.*;
  • char c=(char)System.in.read(); is to enter a single character;

  • int a=cin.nextInt(); is to enter an integer;

  • ##double b=cin.nextDouble(); is to enter an Double-precision floating point number

Give an example:

import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args)throws IOException{
        char c=(char)System.in.read();//输入单个字符
        System.out.println(c);
    }
}

Give another example:


import java.io.*;
import java.util.*;
public class Main{
    public static void main(String[] args)throws IOException{
        Scanner cin=new Scanner(System.in);
         
        int a=cin.nextInt();//输入一个整数
        System.out.println(a);
         
        double b=cin.nextDouble();//输入一个双精度的浮点数
        System.out.println(b);
         
        String str=cin.next();//输入一个单词,遇到分号则输入终止
        System.out.println(str);
         
        String str2=cin.nextLine();//输入一行,中间可有多个空格
        System.out.println(str2);
    }
}

The above is the detailed content of What is the java input statement?. 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
Previous article:How to use java scannerNext article:How to use java scanner