JavaAPI
javaAPI概念:
API(Application Programming Interface,应用程序接口)是一些预先定义的函数,或指软件系统不同组成部分衔接的约定。可以理解为程序员学习的一本字典。通过API的查询,了解到相关类的一些说明,方法,构造方法。
API的使用步骤:
1.打开文档
2.在索引中输入想要查找的类
3.看包的类型,java.lang下的类不许要进行导包步骤,其他包有相应的导包路径
4.看类的解释说明
5.学习成员方法与构造方法
Scanner类
我们可以通过 Scanner 类来获取用户从键盘上的输入。
下面是创建 Scanner 对象的基本语法:
Scanner s = new Scanner(System.in);
对于数字输入的话,使用的是:对象名.nextInt();
对于字符串输入的话,使用的是:对象名.next();
import java.util.Scanner;
/*
* Scanner类的功能:可以实现键盘输入数据
* 引用类型的一般使用步骤:
* 1.导包
* import 包路径.类名;
* 如果需要使用的类,当前类在同一个包下,可以省略导包语句
* 只有java.lang包可以省略导包语句
* 2.创建
* 类名 对象名 = new 类名();
* 3.使用
* 对象名.成员方法名();*/
public class Demo01Scanner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
String str = sc.next();
System.out.println("键盘输入的数字为" + a);
System.out.println("键盘输入的字符为" + str);
}
}
next() 与 nextLine() 区别
next():
1、一定要读取到有效字符后才可以结束输入。
2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
next() 不能得到带有空格的字符串。
nextLine():
1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
2、可以获得空白。
如果要输入 int 或 float 类型的数据,在 Scanner 类中也有支持,但是在输入之前最好先使用 hasNextXxx() 方法进行验证,再使用 nextXxx() 来读取。
Scanner的一些练习
import java.util.Scanner;
/*问题要求:
* 键盘输入两个int数字,并求出和值
*思路:
* 1.键盘输入应该用到Scanner类
* 2.Scanner类三个步骤,导包,创建,使用
* 3.输入的是两个数字,所以要使用两次nextInt
* 4.求和值,进行相加*/
public class Demo02ScannerSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数:");
int a = sc.nextInt();
System.out.println("请输入第二个数:");
int b = sc.nextInt();
int sum = a + b;
System.out.println("输入第一个数是:"+ a +"输入第二个数是:" + b +"两数和为:" + sum);
}
// public int su(){
// int a = sc.nextInt();
// int b = sc.nextInt();
// int sum = a + b;
// System.out.println("输入第一个数是:"+ a +"输入第二个数是:" + b +"两数和为:" + sum);
// return sum;
// }
// }
}
import java.util.Scanner;
/*
* 题目:输入三个数字,求出最大值
* 思路:
* 1.键盘输入,用Scanner
* 2.Scanner三步骤:导包,创建,使用
* 3.应该会调用三次nextInt
* 4.先判断前两个的最大值,再跟第三个数进行比较*/
public class Demo03ScannerMax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入第一个数:");
int a = sc.nextInt();
System.out.println("输入第二个数:");
int b = sc.nextInt();
System.out.println("输入第三个数:");
int c = sc.nextInt();
int temp = a > b ? a : b;
int max = temp > c ? temp : c;
// int max;
// if(a > b){
// max = a;
// }else{
// max = b;
// }
// if(max > c){
// max = max;
// }else{
// max = c;
// }
System.out.println("三个数中最大值为" + max);
}
}
匿名对象
匿名对象就是只有右边的对象,没有左边的名字和赋值运算符
格式 :new 类名称();
使用情况:如果确定有一个对象只能需要使用唯一的一次
/*
* 创建对象格式:
* 类名 对象名 = new 类名();
* 匿名对象就是只有右边的对象,没有左边的名字和赋值运算符
* new 类名称();
* 注意事项:匿名对象只能使用一次,下次用需要新建一个对象
* 使用情况:如果确定有一个对象只能需要使用唯一的一次
* */
public class Demo01Anonymous {
public static void main(String[] args) {
Person per = new Person();
per.name = "金金";
per.showName();
//匿名对象
new Person().name = "金金猪";
new Person().showName();//我叫:null
}
}
匿名对象作为参数或者返回值的情况
package demo.demo02;
import java.util.Scanner;
/**/
public class Demo02Anonymous {
public static void main(String[] args) {
// 普通的使用方式
// Scanner sc = new Scanner(System.in);
// int num = sc.nextInt();
// 匿名对象的使用方式
// int num = new Scanner(System.in).nextInt();
// System.out.println("输入的是" + num);
// 正常导入参数
// Scanner sc = new Scanner(System.in);
// show(sc);
// 用匿名对象作为参数
// show(new Scanner(System.in));
}
// public static void show(Scanner sc){
// }
// 匿名对象作为返回值
public static Scanner show(){
return new Scanner(System.in);
}
}