java中if语句的应用

1. 注释怎么写:

  1.1  //单行注释

  1.2  /*多行注释*/

 

2. If语句的用法

 

2.1.

if(条件语句){

(  ps:只有一行代码时可以不加{}  )

}

 

2.2.

if(条件语句){

 

}else{

 

}

 

2.3.

if(条件语句){

 

}else if{

 

}else{

 

}

例:

public class IfDemo {
public static void main(String[] args) {
//所有方法的运行都是从主方法里运行
//非静态方法不能被静态方法调用
//Debug是以调试的方式运行

//funOne();
//funTwo();
funThree();

}
//1.
public static void funOne(){
int a = 10;
int b= 10;
if(a>=b) {
System.out.println("a大于等于b");
}

}

//2.
public static void funTwo(){
int a = 10;
int b = 6;

//==等号
if(a%b==0) {

System.out.println("b能被a整除");

}else{
System.out.println("b不能被a整除");
}
}

//3.
//判断三个数中的最大值
public static void funThree(){
int a=5;
int b=7;
int c=3;
/*
* if()
* */
if(a>b&&a>c){
System.out.println("a为最大值");
}else if(b>a&&b>c){
System.out.println("b为最大值");
}else{
System.out.println("c为最大值");
}
}


}

 

posted @ 2021-03-15 21:35  轩er  阅读(762)  评论(0)    收藏  举报