Keyboard input and printout in Java

Author: Han Ru

Company: Program Ka (Beijing) Technology Co., LTD

Program Cafe: IT professional skills evaluation platform

Website: www.chengxuka.com

task

1. How to input data by keyboard 2Copy the code

First, keyboard input

Sometimes we need to retrieve input data from the keyboard before the program can proceed. For example, we can design a program to determine whether your grades are passing or not. Everyone’s grades are different. We can choose to input grades on the keyboard, and then determine whether you pass or not. Then you need to learn how to use the keyboard.

The operation syntax of keyboard input is in the superdimensional category here. Because it involves importing packages, creating objects, calling methods, etc. So here, you can only learn to use it. As for its principle, after learning object-oriented, naturally understand.

For a source file structure:

// For a source file structure:

// The first line is the definition of package

package test;

// Step 2: Import the package required by the program

import xxx.xxx.xxx;

// Step 3: class declaration

public classThe name of the class{

	// Step 4: Define the main function: the entry to the program

	pubic static void main(String[] args){
		// Step 5: The code to execute
        
        / /...}}Copy the code

Read the keyboard operation steps:

1.Import the Scanner classimport java.util.Scanner;
2.Create an object of the Scanner class Scanner Input =new Scanner(System.in);
3.It depends on what type of keyboard data you want: input.nextdouble ();// Get a double
	input.nextInt();// Get data of type int
	input.nextBoolean();// Get Boolean data
	input.next();// Retrieves a String.Copy the code

Sample code:

//step1: Import Scanner
import java.util.Scanner;// Import the Scanner class in the java.util package to use Scanenr in your program.

public class Demo19Scanner
{
	public static void main(String[] args){
		//step2: create a scanner object that can read keyboard input
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter your grade (integer) :");

		//step3: read the keyboard input data:
		int score = input.nextInt();// Read an integer entered on the keyboard
		
		System.out.println("What are your grades?" + score);

		String msg = score >= 60? "Pass" : "Fail";
		System.out.println("The results are:"+ msg); }}Copy the code

Running results:

You can also enter other data types on the keyboard:

/ / 1. Import the Scanner
import java.util.Scanner;
public class Demo20Scanner 
{
	public static void main(String[] args) 
	{
		//2. Create Scanner Scanner
		Scanner input = new Scanner(System.in);


		//3. Read the keyboard input data
		System.out.println("Please enter a decimal number:");
		double num1 = input.nextDouble();// Read the decimal double input on the keyboard
		System.out.println("The decimal read is:" + num1);

		

		System.out.println("Please enter a string:");
		String str = input.next();// Read a string entered on the keyboard
		System.out.println("The received string is:" + str);

		System.out.println("Please enter one character:");
		char c = input.next().charAt(0);// Reads a single character typed on the keyboardSystem.out.println(c); }}Copy the code

Running results:

Step pit: want to keyboard input which type of data, it is necessary to call the corresponding method, in the input data, the type must match, otherwise it will be abnormal.

Second, print output

We all know that to print out, use system.out.println (); Statement.

But there are other statements that can also be used for printouts.

Println (); println(); println(); System.out.print(); system.out.print (); Printf (); system.out.printf (); system.out.printf (); // Format prints %d, integer placeholder %s, string placeholder %f, floating point placeholder %.2f,%.3f %c, character placeholderCopy the code

Sample code:

public class Demo21Print 
{
	public static void main(String[] args) 
	{
		/* Print: system.out.xxxx (); Print (); print(); 3, extend content: printf(); Print +format */

		System.out.println("Hello World!");//print + line
		System.out.println("Program coffee!");
		System.out.println();// Just a line break
		System.out.println("King two Dogs");
		System.out.print("Li Xiaohua");
		System.out.print("Liu Tie Zhu");

		System.out.println();

		String name = "Banshee";
		int age = 100;
		double score = 88.7;


		System.out.printf("Name: %s, Age: % D, Score: %.2f\n",name,age,score);// A placeholder, using a symbol, occupies the space
		System.out.println("main.. over.."); }}Copy the code

Running results:

Formatting and printing principle: