一、API
应用程序编程接口,简单来说,API就是厂商提前写好的代码,提前写好的类,类当中会有一些方法供我们进行使用
二、API的使用
1、双击打开或右键打开文档
2、找到索引选项卡中的输入框
3、在输入框中输入 Random,回车
4、先看类在哪个包下
5、看类的描述
6、看构造方法
7、看成员方法
三、Scanner
package api;
import java.util.Scanner;
public class ScannerText {
public static void main(String[] args) {
//1 创建Scanner对象
Scanner sc = new Scanner(System.in);
System.out.println("请输入: ");
//2 调用nextLine方法接收字符串
//ctrl+alt+v:快速生成方法的返回值,光标放在nextLine处即可
//String s = sc.nextLine();
String s2 = sc.next();
//next()和nextLine()都能接收数据,但是前者遇到了空格,就不再接收了,结束标记:空格,tab键
//而后者可以完整的接收,结束标记:回车
System.out.println(s2);
}
}
package api;
import java.util.Scanner;
public class ScannerText2 {
public static void main(String[] args) {
//当nextLine()和nextInt()配合使用的时候,nextLine就没有键盘录入的机会了
//第11行输入的是整数+回车,当整数被num后,还剩下回车,被nextLine使用,直接结束了
//而next()和nextInt()一起使用则不会出现上述情况
//建议:今后在键盘录入的时候,如果是字符串和整数一起接收,建议使用next()
Scanner sc = new Scanner(System.in);
System.out.println("请输入整数: ");
int num=sc.nextInt();
System.out.println("请输入字符串: ");
String s = sc.nextLine();
System.out.println(num);
System.out.println(s);
}
}