流程控制
Scanner
- java 提供了
java.util.Scanner的工具类给我们以获取用户的输入 (java5特征) - 通过Scanner类的
next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据next()读取输入到空格停止,nextLine()读取一行输入
Scanner scanner = new Scanner(System.in);
if (scanner.hasNext()){
String str = scanner.next();
}
scanner.close();
进阶
hasNextInt()判读输入是否整数,nextInt()读取整数数据hasNextFloat()判读输入是否整数,nextFloat()读取整数数据
顺序结构
选择结构
- if
- switch
循环结构
- while
- do while
- for
- 增强型for循环
for - each(java5引入)- 数组
int[] num = {1, 2, 3, 4, 5};
for (int x:num){
System.out.println(x);
}