API: Application programming Interface.
There are many APIs for users to use in Java. Scanner and Random are one of them. The API is actually a class. The scanner class and Random class have been encapsulated. We just need to write them according to their syntax without knowing their basic 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 to import the scanner class in util, and the latter is to import all classes in util)
2. Create object Scanner object name = new Scanner(System.in) / / System.in represents that the source is keyboard (in most cases)
3. Use the object and call its method object name. nextxx() / / call different methods according to the acceptance type
1 import java.util.Scanner; 2 //Input three numbers from the keyboard and output the maximum value 3 public class ScannerDemo { 4 public static void main(String[] args) { 5 Scanner s=new Scanner(System.in); 6 int num=s.nextInt(); 7 System.out.println(num); 8 String str=s.next(); 9 System.out.println(str); 10 int a=s.nextInt(); 11 int b=s.nextInt(); 12 int c=s.nextInt(); 13 int max=a>b?a:b; 14 int endmax=c>max?c:max; 15 System.out.println(endmax); 16 17 } 18 }
Integer -- nextInt(), string -- next(), floating point -- nextFloat()
Class Random
1. import java.util.Random or import java.util. * (the former is the random class in import util, and the latter is all classes in import util)
2. Create Random r=new Random()
3. use
1 import java.util.Random; 2 import java.util.Scanner; 3 //Guess random numbers, only five chances 4 public class DemoRandom { 5 public static void main(String[] args) { 6 Random r=new Random(); 7 Scanner s=new Scanner(System.in); 8 int res=r.nextInt(100);//[0,100) 9 System.out.println(res); 10 int i=0; 11 while (i<5){ 12 System.out.println("Please input the guess number, we will help you judge"); 13 int num=s.nextInt(); 14 if(num>res){ 15 System.out.println("Oh, big."); 16 i++; 17 continue;} 18 else if(num<res){ 19 System.out.println("Small"); 20 i++; 21 continue; 22 } 23 else { 24 System.out.println("Guess right."); 25 i++; 26 break; 27 } 28 } 29 if(i==5) 30 System.out.println("You've run out of times"); 31 else 32 System.out.println("Congratulations, you used it"+i+"second"); 33 34 } 35 }
Object name. Method () / / r.nextInt(), i.e. randomly generate a number with an integer range -------- r.next(n) / / randomly generate a value between [0,n) (left closed and right open)
Anonymous object: that is, it can only be used once without naming the object. The next time it is used, it is a new anonymous object, which can be used as the parameter of the function and the return value of the function (new class name ())
1 import java.util.Scanner; 2 3 /*Anonymous object as parameter, return value 4 */ 5 public class DemoAnonymous { 6 public static void main(String[] args) { 7 meth(new Scanner(System.in)); 8 Scanner s=meth2(); 9 int num=s.nextInt(); 10 System.out.println(num); 11 } 12 public static void meth(Scanner sc){ 13 int num=sc.nextInt(); 14 System.out.println(num); 15 } 16 public static Scanner meth2(){ 17 return new Scanner(System.in); 18 } 19 }
Line 7 takes an anonymous object as a parameter, and line 17 returns an anonymous object.