class IFDemo1{
public static void main(String[] args) {
/* if(结果是布尔类型的值或者说是表达式){
执行的语句体
}
*/
int a=1;
if(a++>1){ //()里面的结果为true 那就执行{}里面的语句体
System.out.println("Hello World!");
}
System.out.println("Hello World!下面的代码");
}
}
2、if…else比较数的大小
class IFDemo2 {
public static void main(String[] args) {
if(5>2){
System.out.println("Hello World!真");
}else{
System.out.println("Hello World!假");
}
System.out.println("Hello World!下面的代码");
}
}
3、求两个数的最大值
class IFDemo3 {
public static void main(String[] args) {
// if else 来求出两个数的最大值
int a=20;
int b=100;
//定义一个变量来保存这个最大值
int max=0;
if(a>b){
max=a;
}else{
max=b;
}
//输出最大值
System.out.println("最大值是"+max);
}
}
4、if…else 嵌套求三个数的最大值
class IFDemo4{
public static void main(String[] args) {
int a=20;
int b=200;
int c=20000;
int max=0;
//if else 嵌套
if(a>b){
if(a>c){
max=a;
}else{
max=c;
}
}else{
if(b>c){
max=b;
}else{
max=c;
}
}
System.out.println("最大值是"+max);
}
}
5、三个数求最大值的两种方法
class IFDemo5 {
public static void main(String[] args) {
int a=20;
int b=10;
int c=5;
int max=0;
int max2=0;
/*
if(a>b){
max=a;
}else{
max=b;
}
if(max>c){
max2=max;
}else{
max2=c;
}*/
if(a>b&&a>c){
max=a;
}
if(b>a&&b>c){
max=b;
}
if(c>a&&c>b){
max=c;
}
System.out.println("最大值是"+max);
}
}