directory

  • Learning materials
  • User interaction Scanner class

Learning materials

B station crazy god said: www.bilibili.com/video/BV12J…

User interaction Scanner class

package com.zy7y.scanner;

import java.util.Scanner;

/ * * *@ProjectName: JavaSE
 * @PackageName: com.zy7y.scanner
 * @Author: zy7y
 * @Date: 2020/8/14 12:09 PM *@Description: User interaction, keyboard */
public class ScannerStudy {
    public static void main(String[] args) {
        // Create a scanner object that accepts keyboard data
        Scanner scanner = new Scanner(System.in);

        / / way
// system.out. print(" Next to receive :");
//
// // Check whether there is any input
// if (scanner.hasNext()) {
// // scanner.nextline () returns all characters before enter enter, scanner.next(). Only valid characters are read, and Spaces before valid input are automatically filtered
// String inputContext = scanner.next();
Println (" Input :" + inputContext); // system.out.println (" input :" + inputContext);
/ /}

        // The first option is this one
        System.out.print("Enter content using nextLine :");
        //String inputContext = scanner.nextLine();
        String inputContext = scanner.next();
        System.out.println(inputContext);

// // can only accept numeric input from the user, and the received object is an integer
// scanner.nextInt();
// // receives a String object
// scanner.next();

        // Close the stream objectscanner.close(); }}Copy the code

Scanner Calculates the number of digits entered by the user and their sum

 // Count the number and value of the user input
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("Please enter digital content, if you want to exit, just enter non-digital content:");
        // Count the input number
        int count = 0;

        // Add the numbers
        int sum = 0;
        while (scanner1.hasNextInt()){
            count ++;
            int number = scanner1.nextInt();
            sum += number;
        }
        scanner1.close();
        System.out.println("You have opted out, enter number of numbers now" + count + ", sum of current numbers" + sum);
Copy the code