注意事项
三元运算符可以实现的都可以使用if 语句代替
区别:
三元运算符比较简洁
三元运算符必须返回结果
if语句可以执行多条语句
注意事项
三元运算符可以实现的都可以使用if 语句代替
区别:
三元运算符比较简洁
三元运算符必须返回结果
if语句可以执行多条语句
特殊的用法:
if()
Xxx;
Xxx;
public class IfDemo{
public static void main(String[] args){
int hp = 3;
if(hp<=0)
System.out.println("已经死亡");//if语句判定条件
System.out.println("GameOver");
//if(hp<=0){
//System.out.println("已经死亡");
//}else{
//System.out.println("还活着");
//}
//if(hp>0){
//System.out.println("还活着");
//}0-1000
//0-100 D
//101-300 C
//301-500 B
//501-600 A
//601-700 S
//701-800 SS
//801-1000 SSS
int score = 800;
if(score >= 0 && score<=100 ){
System.out.println("你获得的评级是:D");
}
if(score >= 101 && score<=300 ){
System.out.println("你获得的评级是:C");
}
if(score >= 301 && score<=500 ){
System.out.println("你获得的评级是:B");
}
if(score >= 501 && score<=600 ){
System.out.println("你获得的评级是:A");
}if(score >= 601 && score<=700 ){
System.out.println("你获得的评级是:S");
}if(score >= 701 && score<=800 ){
System.out.println("你获得的评级是:SS");
}if(score >= 801 && score<=1000 ){
System.out.println("你获得的评级是:SSS");
}
}
}
if();
public class IfDemo{
public static void main(String[] args){
int hp = 3;
if(hp<=0);//此时if语句为结束语句
System.out.println("已经死亡");
System.out.println("GameOver");
//if(hp<=0){
//System.out.println("已经死亡");
//}else{
//System.out.println("还活着");
//}
//if(hp>0){
//System.out.println("还活着");
//}0-1000
//0-100 D
//101-300 C
//301-500 B
//501-600 A
//601-700 S
//701-800 SS
//801-1000 SSS
int score = 800;
if(score >= 0 && score<=100 ){
System.out.println("你获得的评级是:D");
}
if(score >= 101 && score<=300 ){
System.out.println("你获得的评级是:C");
}
if(score >= 301 && score<=500 ){
System.out.println("你获得的评级是:B");
}
if(score >= 501 && score<=600 ){
System.out.println("你获得的评级是:A");
}if(score >= 601 && score<=700 ){
System.out.println("你获得的评级是:S");
}if(score >= 701 && score<=800 ){
System.out.println("你获得的评级是:SS");
}if(score >= 801 && score<=1000 ){
System.out.println("你获得的评级是:SSS");
}
}
}
注意事项
三元运算符可以实现的都可以使用if语句代替
区别:
三元运算符比较简洁
三元运算符必须返回结果
if语句可以执行多条语句
public class IfDemo{
public static void main(String[] args){
//int hp = 3;
//if(hp<=0);
//System.out.println("已经死亡");
//System.out.println("GameOver");
//if(hp<=0){
//System.out.println("已经死亡");
//}else{
//System.out.println("还活着");
//}
//if(hp>0){
//System.out.println("还活着");
//}0-1000
//0-100 D
//101-300 C
//301-500 B
//501-600 A
//601-700 S
//701-800 SS
//801-1000 SSS
/*
int score = 800;
if(score >= 0 && score<=100 ){
System.out.println("你获得的评级是:D");
}
if(score >= 101 && score<=300 ){
System.out.println("你获得的评级是:C");
}
if(score >= 301 && score<=500 ){
System.out.println("你获得的评级是:B");
}
if(score >= 501 && score<=600 ){
System.out.println("你获得的评级是:A");
}if(score >= 601 && score<=700 ){
System.out.println("你获得的评级是:S");
}if(score >= 701 && score<=800 ){
System.out.println("你获得的评级是:SS");
}if(score >= 801 && score<=1000 ){
System.out.println("你获得的评级是:SSS");
}
*/
int a = 9 ,b =20;
int max = a>b?a:b;
if(a>b){
max = a;
}else{
max=b;
}
}
}
第三章 流程控制
50课时if语句的特殊控制
if()
sys();
sys();
1-当if语句后没有大括号时,默认后面第一句为if语句块中执行的语句。
其他语句一定会执又分哈 行.
if();
sys;
sys;
2-当if语句后面有分号时说明已经结束
public class If{
public static void main (String [] args ){
//int hp = 100;
//if(hp<=0){
//System.out.println("菜");
//}
// if(hp<=0){
// System.out.println();
// }
//if(hp>0){
//System.out.println();
//
//if(hp>0){
// System.out.println("淦");
//}
//else{
// System.out.println("菜");
//}
/** int score = 80;
if(score>=0 && score <=100){
System.out.println("你这个弟弟");
}
else if (score>=101 && score <= 300){
System.out.println("还是弟弟");
}
else if (score>=301 && score <= 500){
System.out.println("还是弟弟");
}
else if (score>=501 && score <= 700){
System.out.println("还是弟弟");
}
*/
int a=9,b=20;
int max=a>b?a:b;
if(a>b){
max = a;
}
else{
max = b;
}
System.out.println(max);
int c= 100,d= 150;
int g=c>d?c:d;
if(c>d){
g = c;
}
else{
g = d;
}
System.out.println(g);
}
}
如果if语句后面没有大括号的话,默认后面跟着的第一个语句就是if的语句块
if 语句
public class IfDemo {
public static void main (String[] args) {
int hp=13413;
if(hp<=0){
System.out.println("Game over");
} else {System.out.println("You win");
}
}
}
if括号后面加分号,相当于直接结束语句。if后面不能直接加分号。