Scanner
内涵
体现了人机交互,可以通过scanner获取用户的输入
基本语法
- Scanner s=New Scanner(System.in)
- 通过scanner的next和nextLine方法获取输入的字符串,读取前使用hasNext和hasNextLine判断是否还有输入的数据
- next和nextLine的区别在于前者以空白作为结束符后者以回车作为结束符
代码示例:
public static void main(String[] args) {
//创建一个扫面器对象,用于接收键盘数据
Scanner scanner=new Scanner(System.in);
System.out.println("");
//判断用户有没有输入字符串
if(scanner.hasNextLine())
{
//使用nextLine方式接受
String str=scanner.nextLine();
System.out.println(str);
}
//属于IO类不关闭会一直占用资源,要记得关闭
scanner.close();
}
- scanner.hasNext可以针对某个类型去做判断,同理next也可以针对某个类型去输出,例如nextInt和hasInt
选择结构
if和switch
switch case记得加break,以防代码穿透现象
idea自带反编译功能,可以通过 ctrl+alt+shift+s文件打开项目结构,打开后查看class文件,把class文件通过idea打开,就能自动反编译
循环结构
while、dowhile、for
break和continue
break 直接退出循环
continue 跳出某次循环