流程控制
1 用户交互Scanner
基本语法 Scanner s = new Scanner(System.in);
Srting str = scanner.next()
String str = scanner.nextLine()
scanner.close()
Scanner scanner=new Scanner(System.in);
//从键盘输入数据
int i = 0;
float f = 1.1f;
System.out.print("请输入一个整数:");
if(scanner.hasNextInt()){
i= scanner.nextInt();
System.out.println(i);
}else {
System.out.println("你输入的不是整数!");
}
System.out.print("请输入一个小数:");
if(scanner.hasNextFloat()){
f= scanner.nextFloat();
System.out.println(f);
}else {
System.out.println("你输入的不是小数!");
}
scanner.close();
2 顺序结构
从上到下依次进行
3 选择结构
if
switch byte,short ,int ,char String(jdk7) case标签必须为字符常量或字面量
switch(expression){
case value:
break;
case value:
break;
default:
}
public class Demo04 {
public static void main(String[] args) {
char c = 'A';
//case 穿透 没有break 符合条件后面都会进行
switch (c){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
default:
System.out.println("你是垃圾");
}
4 循环结构
while 循环
do …while循环(循环至少能进行一次,先执行后判断)
for循环
增强for
for(声明语句:表达式){//代码句子 }
用来遍历数组和集合
5 break&continue
int i = 0;
while (i<100){
i++;
if(i%10==0){
System.out.println();
break;
//continue;
}
System.out.print(i);
}