Use java output statement _Java common input and output statement

I. Overview

Input and output can be said to be the basic functions of a computer. As a language system, java is mainly implemented in accordance with the mode of stream. The flow of data is determined according to the direction of the computer. The data stream flowing into the computer is called the input stream (inputStream), and the data stream sent by the computer is called the output stream (outputStream).

In the Java language system, the main operations on the data stream are encapsulated in the java.io package, and the input and output operations of the computer can be realized through the classes in the java.io package. When writing the input and output operation code, you need to use the import statement to import the java.io package into the class where the application is located, and then you can use the classes and interfaces in java.io

Second, the input sentence

1. Use the Scanner class:

(1) Use the java.util package. import java.util.*;

(2) Construct the Scanner class object, which is attached to the standard input stream System.in. Scanners = new Scanner(System.in);

(3) Commonly used next() method series: nextInt(): input integer nextLine(): input string nextDouble(): input double precision number next(): input string (with space as separator).

import java.util. *;

public class DEMO_1 {

public static void main(String[] args){

Scanner s = new Scanner(System.in);

System.out.print("Enter your name: ");

String name = s.nextLine();

System.out.print("Enter your age: ");

int age = s.nextInt();

System.out.println("Name: " + name + " Age: " + age );

s.close(); //A warning will appear if the Scanner object is not closed

}

}

Note: Line 1 of the code creates an object of class Scanner, which is used for input. The following code takes a value from the console input and assigns it to the corresponding variable.

2.使用java.io.BufferedReader和java.io.InputStreamReader:

step:

(1) Use the java.io package. import java.io.*;

(2) Construct a BufferedReader class object, which is attached to the standard input stream System.in.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public class Sandbox4 extends Sandbox2{

public static void main(String[] args) {

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String str = null;

while(true){

try {

str = br.readLine();

}catch(IOException e){

e.printStackTrace ();

}

if(str.equals("END"))break;

System.out.print(str);

}

}

}

caution:

(1) read method: read a single character. Returns: the character read as an integer ranging from 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached;

(2) readLine method: read a text line. A line is considered terminated by one of the following characters: a newline ('\n'), a carriage return ('\r'), or a carriage return followed by a newline. Returns: a string containing the contents of the line, without any line terminators, or null if the end of the stream has been reached.

3 、 使用 java.io.Console :

public class Sandbox4 extends Sandbox2{

public static void main(String[] args) {

Console console = System.console();

if (console == null) {

throw new IllegalStateException("Console is not available!");

}

String str = null;

while(true){

str = console.readLine("请输入");

if("END".equals(str))break;

System.out.println(str);

}

}

}

三、输出语句

Java中的输出语句有以下四种:

System.out.println(1111);//换行打印

System.out.print(1111);//不换行打印

System.out.write(2222);//字节输出

System.out.printf("%+8.3f\n", 3.14);//按格式输出

System.out.println(); 是最常用的输出语句,它会把括号里的内容转换成字符串输出到输出窗口(控制台),并且换行,当输出的是一个基本数据类型时,会自动转换成字符串,如果输出的是一个对象,会自动调用对象的toString();方法,将返回值输出到控制台

System.out.print(); 与第一个很相似,区别就是上一个输出后会换行,而这个命令输出后并不换行。

System.out.printf(); 这个方法延续了C语言的输出方式,通过格式化文本和参数列表输出。

---------------------

作者:鹿鸣野

来源:CSDN

原文:https://blog.csdn.net/baidu_41666198/article/details/79942661

版权声明:本文为博主原创文章,转载请附上博文链接!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324136072&siteId=291194637