1.流程控制语句:顺序、选择、循环
2.选择结构
If(条件){
语句1;
语句2;
…}
If(条件){
语句1;
语句2;
…}
else{
语句1;
语句2;
…}
代码:
package com.imooc.flow;
import java.util.Scanner;
public class ScoreAssess {
public static void main(String[] args) {
//成绩大于等于90输出优,大于等于80小于90输出良,大于等于60小于80输出中,小于60输出不及格
System.out.println("Please input score");
Scanner sc=new Scanner(System.in);
int score=sc.nextInt();
if(score>=90)
{System.out.println("It is excellent");}
if(score<90&score>=80)
{System.out.println("It is good");}
if(score<80&score>=60)
{System.out.println("It is mediun");}
if(score<60)
{System.out.println("It is fail");}
if(score>=90)
{System.out.println("It is excellent");}
else if(score>=80)//amount to score>=80&score<90
{System.out.println("It is good");}
else if(score>=60)
{System.out.println("It is mediun");}
else
{System.out.println("It is fail");}
}
}
3.嵌套if结构
代码:
package com.imooc.flow;
import java.util.Scanner;
public class IntCompare {
public static void main(String[] args){
System.out.println("Please input an integer a");
Scanner cs=new Scanner(System.in);
int a=cs.nextInt();
System.out.println("Please input an integer b");
Scanner sc=new Scanner(System.in);
int b=sc.nextInt();
if(a>b){
System.out.println("int:"+a+">"+"int:"+b);
};
if(a==b){
System.out.println("int:"+a+"="+"int:"+b);
};
if(a<b){
System.out.println("int:"+a+"<"+"int:"+b);
};
if(a!=b){
if(a>b){
System.out.println(a+">"+b);
}else{
System.out.println(a+"<"+b);
}
}else{
System.out.println(a+"="+b);
}
}
}
4.if和switch的区别
If结构:
判断条件是布尔类型
判断条件是一个范围
switch结构:
判断条件是常量值
switch结构:
switch(表达式){
case 常量表达式1:
语句1;break;
case 常量表达式2:
语句2;break;
default:
语句3;
}
代码:
package com.imooc.flow;
import java.util.Scanner;
public class WeekDemo1 {
public static void main(String[] args) {
System.out.println("Please input an integer between 1 and 7:");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
switch (a) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("The integer: " + a + " is not between 1 and 7!");
}
}
}
代码:
package com.imooc.flow;
import java.util.Scanner;
public class WeekDemo2 {
public static void main(String[] args) {
System.out.println("Please input week");
Scanner sc = new Scanner(System.in);
String week = sc.next();
week=week.toUpperCase();//改为大写
switch (week) {
case "MONDAY":
System.out.println("星期一");
break;
case "TUESDAY":
System.out.println("星期二");
break;
case "WEDNESDAY":
System.out.println("星期三");
break;
case "THURSDAY":
System.out.println("星期四");
break;
case "FRIDAY":
System.out.println("星期五");
break;
case "SATURDAY":
System.out.println("星期六");
break;
case "SUNDAY":
System.out.println("星期日");
break;
default:
System.out.println("The word: " + week + " is wrong!");
}
}
}