Home > Java > javaTutorial > What is the java input and output statement?

What is the java input and output statement?

青灯夜游
Release: 2020-10-21 15:59:53
Original
23466 people have browsed it

In java, the input statement is "Scanner object.next() series method", for example "Scanner object.nextLine()" represents the input string; the output statement is "System.out.println()" , "System.out.print()", etc.

What is the java input and output statement?

For those who often answer questions on the computer, they must first solve the input and output methods. The input and output streams of Java will only be exposed in the later part of the Java learning process. , but we can master some simple, commonly used input and output methods

Output stream

There are three commonly used output statements in java:

  • System.out.println();//Printing with newline, it will automatically wrap after output

  • System.out.print();//Print without newline

  • System.out.printf();//Output according to format

Output example

public class test {
    public static void main(String []args){
    
    System.out.println(1111);//换行打印,输出后自动换行
    System.out.print(1111);//不换行打印
    System.out.printf("分数是:%d",88);//按格式输出
    }
}
Copy after login

Input stream

Java's input needs to rely on the Scanner class:

 import java.util.Scanner;
Copy after login

If input is required, declare a Scanner object first:

Scanner s = new Scanner(System.in);
Copy after login

Scanner is attached to the input stream System.in , after declaring the Scanner object, you need to use the next() method series to specify the input type when inputting, such as input integer, input string, etc.

Commonly used next() method series:                                                                                                       String

  • nextDouble(): Input a double precision number

  • next(): Input a string (use spaces as delimiters).

  • Input example

    import java.util.Scanner;
    public class test {
        Scanner s = new Scanner(System.in);  // 声明Scanner的一个对象
        System.out.print("请输入名字:");
        String name = s.nextLine();
        System.out.println(name);
        
        System.out.print("请输入年龄:");
        int age = s.nextInt();
        System.out.println(age);
        
        System.out.print("请输入体重:");
        double weight = s.nextDouble();
        System.out.println(weight);
        
        System.out.print("请输入学校:")
        String school = s.next();
        System.out.println(school);
        
        s.close();    // 关闭输入流,若没有关闭则会出现警告
        }
    }
    Copy after login
  • The output is as follows:
  • 请输入名字:梁 十 安
    梁 十 安
    请输入年龄:18
    18
    请输入体重:70.5
    70.5
    请输入学校:xxx大学 阿斯顿
    xxx大学
    Copy after login

    The above is the detailed content of What is the java input and output statement?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template