Java Chapter 3 Input and Output

First write this code in Main.java:

public class Main {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int a = input.nextInt();
        System.out.println(a);
		input.close();
	}
	
}

At this time, press ctrl+f11 to run it? Haha, it is wrong;

After pressing ctrl+shift+o in your code, something like this will appear on the first line of code:

import java.util.Scanner;

This thing is used to import java packages, which can be simply understood as C++ header files;

The Scanner class is what Java uses for input. It needs to import the java package before it can be used;

The shortcut ctrl+shift+o mentioned earlier is more powerful, it will automatically add the packages needed to run the code to the first line, and automatically delete the unnecessary ones;

Next, let's focus on this Scanner:

Scanner input = new Scanner(System.in);

Input can be understood as a variable that can read keyboard input;

If you want to read something from input, you need some additional functions:

int a = input.nextInt(); read an int integer

Long a = input.nextLong(); read long integer

String s = input.next(); read a string

String s = input.nextLine(); read a line

String s = input.next();

char a = s.charAt(0); Since Scanner cannot read a single character, read a single character like this

Note: When the input is completed, add: input.close(); indicates the end of the input

Then let's talk about the output of Java:

System.out.println(); output and wrap

System.out.printf(); This is more powerful, it is compatible with c++ printf

Guess you like

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