if语句:
格式:
if(关系表达式){
语句体;
}
案例:
public class ConstantDemo{
public static void main(String[] args) {
//定义两个变量
int a=1;
int b=2;
int max;
//使用if语句进行判断,如果a>b,则最大值为a,若a<b,则最大值为b
if (a>b) {
max =a;
System.out.println(a);
}
System.out.println(b);
System.out.println("结束");
}
}
执行流程:
1.首先计算关系表达式的值
2.如果关系表达式的值为true就执行语句体
3.如果关系表达式的值为false就不执行
4.继续执行后面的语句内容
if…else语句:
格式:if(关系表达式){
语句体1;
}else{
语句体2;
}
执行流程
1.首先计算关系表达式的值
2.如果关系表达式的值为true就执行语句体1
3.如果关系表达式的值为false就执行语句体2
4.继续执行后面的语句内容
案例:
import java.util.scanner; //导入控制台的数据
public class ConstantDemo{
public static void main(String[] args) {
//给出一个整数
scanner sc=new scanner