
我们可以选择简单的语句判断,也可以对多个条件进行同时的筛选。在php中除了switch可以判断两个以上的条件外,if…elseif…else语句也同样可以实现。认为switch语句难度大的,不妨来学习一些这个语句。下面我们就php中if…elseif…else的概念、语法带来讲解,最后分享成绩的实例判断。
1.概念
if…elseif…else语句根据两个以上的条件执行不同的代码。
2.语法
if(条件){
条件为true时执行的代码;
}elseif(condition){
条件为true时执行的代码;
}else{
条件为false时执行的代码;
}
3.实例
判断分数对应的等级。
/*编写一个程序,按照下列分数段指定相应的等级,例如:
*90~100A级
*80~89B级
*70~79C级
*60~69D级
*0~59E级
*其他不合法
*使用if…elseif…else语句实现判断95分对应的等级。
*/
publicclassClassify{
publicstaticvoidmain(String[]args){
intscore=95;
if(score>=90&&score<=100){
System.out.println("A级");
}
elseif(score>=80&&score<=89){
System.out.println("B级");
}
elseif(score>=70&&score<=79){
System.out.println("C级");
}
elseif(score>=60&&score<=69){
System.out.println("D级");
}
elseif(score>=0&&score<=59){
System.out.println("E级");
}
else{
System.out.println("不合法");
}
}
}原文来自:https://www.py.cn © 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END



















































暂无评论内容