Analysis of API of Scanner and Random Anonymous Objects Commonly Used in Java

  • 2021-07-09 08:19:40
  • OfStack

API: Application programming Interface, application programming interface.

Java encapsulates a lot of API for users to use, Scanner and Random is one of them, API is actually a class, has encapsulated Scanner class, Random class, we only need to write according to its syntax, do not need to know its fundamental source code

Class Scanner:

1. To use the Scanner class, you need to import its package, import java. util. Scanner or import java. util. * (the former is the Scanner class in util, the latter is all the classes in util)
2. Create object Scanner Object name = new Scanner (System. in)//System. in means that the source is a keyboard (in most cases)
3. Use the object and call its method object name. nextxx ()//Call different methods depending on the accepted type


import java.util.Scanner;
// Enter from the keyboard 3 Number, output maximum value 
public class ScannerDemo {
  public static void main(String[] args) {
    Scanner s=new Scanner(System.in);
    int num=s.nextInt();
    System.out.println(num);
    String str=s.next();
    System.out.println(str);
    int a=s.nextInt();
    int b=s.nextInt();
    int c=s.nextInt();
    int max=a>b?a:b;
    int endmax=c>max?c:max;
    System.out.println(endmax);

  }
}

Shaping--nextInt (), String--next (), Floating--nextFloat ()......

Class Random

1. Guide package import java. util.Random or import java.util.* (the former is the Random class imported into util, the latter is all classes imported into util)
2. Create Random r=new Random ()
STEP 3 Use


import java.util.Random;
import java.util.Scanner;
// Guess random numbers, only 5 A second chance 
public class DemoRandom {
  public static void main(String[] args) {
    Random r=new Random();
    Scanner s=new Scanner(System.in);
    int res=r.nextInt(100);//[0,100)
    System.out.println(res);
    int i=0;
    while (i<5){
      System.out.println(" Please enter the guessed numbers and we will help you judge ");
      int num=s.nextInt();
      if(num>res){
        System.out.println(" Big, oh ");
        i++;
        continue;}
      else if(num<res){
        System.out.println(" It's small ");
        i++;
        continue;
      }
      else {
        System.out.println(" Guess right ");
        i++;
        break;
      }
    }
    if(i==5)
      System.out.println(" You run out of times ");
    else
      System.out.println(" Congratulations, you used it "+i+" Times ");

  }
}

Object name. Method ()//r. nextInt () is to randomly generate a number in the shaping range--------r. next (n)//Randomly generate a number between [0, n) (closed left and open right)

Anonymous object: Namely does not need to give the object name, can only use 1 time, the next time uses again is a new anonymous object, may make the function parameter, the function return value (new class name ())


import java.util.Scanner;

/* Anonymous object as a formal parameter and returns a value 
*/
public class DemoAnonymous {
  public static void main(String[] args) {
    meth(new Scanner(System.in));
    Scanner s=meth2();
    int num=s.nextInt();
    System.out.println(num);
  }
  public static void meth(Scanner sc){
    int num=sc.nextInt();
    System.out.println(num);
  }
  public static Scanner meth2(){
    return new Scanner(System.in);
  }
}

7-line anonymous object as a formal parameter, and 17-line returns 1 anonymous object.


Related articles: