Input and output data from the command line

1. Enter the basic data type

Scanner is in the java.util package, you can use this class to create an object.

Scanner reader = new Scanner(System.in);

Then the reader object calls the following methods to read various basic types of data entered by the user on the command line:

nextBoolean();nextByte(),nextShort(),nextInt(),nextLong(),nextFloat(),nextDouble()

reader target blank as separator mark read user data input on the command line.

When the above method is executed, a blockage state (WAITING) may occur when reading data . If there is still " data " in the keyboard buffer to read, there will be no blockage when the above method is executed, otherwise the program needs to wait for the user to input new data on the command line and press enter to confirm (Enter will refresh the keyboard buffer The content in, eliminate the jam state).

import java.util.Scanner;
class A 
{
	public static void main(String[] args) 
	{
		Scanner reader = new Scanner(System.in);
		int i = reader.nextInt();
		System.out.println(i);
		i = reader.nextInt();
		System.out.println(i);
	}
}

Let the reader objects first call hasNextInt () to determine the next months data whether is in line with nextInt () the requested data, if they meet the requirements, hasNextInt () method that returns to true , whether those returns false . Note that when the nextInt () successfully read data after , hasNextInt () method next determines only one data .

import java.util.Scanner;
class A 
{
	public static void main(String[] args) 
	{
		Scanner reader = new Scanner(System.in);
		System.out.println("用空格或回车做分隔,输入若干个数,最后输入#结束,\n然后回车确认。");
		double sum = 0;
		int m = 0;
		while(reader.hasNextDouble()){
			double x = reader.nextDouble();
			m = m + 1;
			sum = sum + x;
		}
		System.out.println(m + "个数和为:" + sum);
	}
}

2. Output basic data types

System.out.println () or System.out.print () can output string values ​​and expression values. The difference between the two is that the former wraps after outputting data, while the latter does not wrap.

Concatenation symbol is allowed: "+" concatenates a variable, expression or a constant value with a string and outputs it, such as:

System.out.println (m+" The sum of the number is "+sum);

System.out.println(":"+123+"大于"+122)

JDK5 adds a new data output method similar to the printf function in C language :

System.out.printf (" format control part " , expression 1 , expression 2 , ... expression n)

The format control part is composed of format control symbols: %d , %c , %f , %s and ordinary characters, and ordinary characters are output as they are. Format symbols are used to output the value of an expression.

%d : Output int type data value.

%c : output char data.

%f : Output floating-point data, with a maximum of 6 decimal places .

%s : Output string data.

When outputting data, you can also control the position of the data on the command line, for example:

%md : The output int type data occupies m columns.

%m.nf : The output floating-point data occupies m columns, and the decimal point retains n places.

 

Guess you like

Origin blog.csdn.net/qq_43629083/article/details/108608905
Recommended