[Java course experience] The Scanner method of receiving user input in Java includes single character (keyboard entry)

Preface

We programmers will not get the input content, but Java has already done the keyboard input function for us, we only need to use the finished function

1. The three steps of using the keyboard to enter

①. Guide package: import java.util.Scanner; Import the code that has been written in Java

②Create object: Scanner sc = new Scanner(System.in); fixed writing

③Use object: sc.nextInt(); here is only to get the integer entered by the keyboard

Code:

import java.util.Scanner;

public class ScannerDemo01 {
    
    
	public static void main(String[] args) {
    
    
		// 2.创建对象: Scanner sc = new Scanner(System.in); 固定写法
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入一个数字: ");
		// 3.使用功能: sc.nextInt(); 获取键盘输入的数字
		int num = sc.nextInt();
		System.out.println("用户输入的数字是: " + num);
	}
}

Two, keyboard input integer, decimal, character, string

1. Input format

①nextInt(): receive integer
②nextDouble(): receive decimal
③next(): can receive string, can't receive carriage return, no spaces
④nextLine(): can receive string, can also receive carriage return alone , can also receive spaces
⑤next().charAt(0): Receive a single character

import java.util.Scanner;
public class ScannerDemo03 {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		
		System.out.println("请输入字符串nextLine: ");
		String str = sc.nextLine(); // 接收字符串
		System.out.println("str = " + str); //  
		
		System.out.println("请输入整数: ");
		int a = sc.nextInt();
		System.out.println("a = " + a);
		
		System.out.println("请输入小数: ");
		double d = sc.nextDouble(); // 3.33回车
		System.out.println("d = " + d);
		
		System.out.println("请输入字符串next: ");
		String str2 = sc.next(); // next接收用户输入的字符串
		System.out.println("str2 = " + str2);
		
		System.out.println("请输入字符: ");
        char ca = sc.next().charAt(0);//接收用户输入的单字符
        System.out.println("ca = " + ca);
	}
}

2. Matters needing attention

When nextLine() is used after nextInt(), etc., it will not be able to input .

Code:

import java.util.Scanner;
public class Test05 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入整数: ");
        int a = sc.nextInt();
        System.out.println("a = " + a);

        System.out.println("请输入字符串nextLine: ");
        String str = sc.nextLine();
        System.out.println("str = " + str);
    }

The results of the calculation are shown in the figure below

Users can't
-The user cannot enter the value of str

the reason:

The user has not had time to enter str in the future. The program will end. The reason is that nextLine() can accept carriage returns. When entering 10 in the previous step, press Enter to enter, but nextInt() only enters 10, and the carriage return is nextLine() Accepted, so the user cannot enter

Solution:
①. Enter nextLine() at the very beginning

② Replace nextLine() with next(), next() can also receive strings but not spaces and carriage returns

3. Explanation of the method of entering a single character:

The Scanner class does not define a method to read char, and there is no such thing as nextChar(). Therefore, if we want to enter a single string, we can only start with the String type, so call the charAt(0) method of String to get the first character.

Complement: String.charAt() method: return the char value at the specified index

Guess you like

Origin blog.csdn.net/maikotom/article/details/108859887