一、单分支 if 语句
-
语法格式
-
...A if(条件){ B 执行语句C; } ...D
-
当且仅当B为 true 时执行C,执行D
-
执行顺序
if(radius>=0){//当半径大于等于0时,才能执行并计算圆的面积
area=radius*radius*PI
System.out.println("The area for the circle of radius "+radius+" is "+area);
}
二、双分支 if-else 语句
-
语法格式
-
...A if(条件){ B 执行语句C; }else{ 执行语句D; } ...E
-
当B为 true 时执行C,执行E
当B为 false 时执行D,执行E -
执行顺序
三、嵌套的 if 语句
-
语法格式
-
...A if(条件1){ B ...C if(条件2){ D ...E } ...F }else{ ...G } ...H
-
当B为true时执行C,继续判定D,当D再为true时执行E,执行F,执行H;
-
当B为false时执行G,执行H
-
执行顺序
四、多分支 if-else 语句
-
语法格式
-
...A if(条件1){ B ...C }else if(条件2){ D ...E }else{ ...F } ...G
-
当B为true时执行C,执行G
-
当B为false时,继续判定D,当D为true时执行E,执行G;当B为false时,继续判定D,当D为false时执行F,执行G
-
执行顺序
import java.util.*;
class Demo08{
/*
数据:随机产生三位数,用户输入三位数,拆分出 a b c d e f
步骤:
1.随机产生三位数
random.nextInt(900)+100;
2.用户输入三位数
3.将随机产生的数拆分为a b c,用户输入的数拆分为d e f
4.如果“abc=def,10000”,“(d==a&&e==c&&f==b)||(d==b&&e==a&&f==c)||(d==b&&e==c&f==a)||(d==c&&e==a&&f==b)||(d==c&&e==b&&f==a)3000”
“a=d e f b=d e f c=d e f 1000”
*/
public static void main(String[] args){
Random random=new Random();
int number1=random.nextInt(900)+100;//[100,1000)
System.out.println(number1);
Scanner scanner=new Scanner(System.in);
System.out.print("请输入一个三位数:");
int number2=scanner.nextInt();
int num1=number1;//566
int num2=number2;
int c=num1%10;//6
num1/=10;//56
int b=num1%10;//6
num1/=10;//5
int a=num1%10;//5
int f=num2%10;
num2/=10;
int e=num2%10;
num2/=10;
int d=num2%10;
if(number1==number2){ // a b c
System.out.println("一等奖10000美元"); // d e f
}else if((d==a&&e==c&&f==b)||(d==b&&e==a&&f==c)||(d==b&&e==c&&f==a)||(d==c&&e==a&&f==b)||(d==c&&e==b&&f==a)){
System.out.println("二等奖3000美元");
}else if(d==a||d==b||d==c||e==a||e==b||e==c||f==a||f==b||f==c){
System.out.println("三等奖1000美元");
}else{
System.out.print("再来一次!");
}
}
}
五、switch 语句
-
语句格式
-
switch(switch表达式){ case 值1:语句(组) 1; break; case 值2:语句(组) 2; break; ... case 值N:语句(组) N; break; default:默认情况下执行的语句(组); }
-
switc语句遵从以下规则:
①switch表达式能计算出一个char、byte、short、int或者String型值,并且必须用括号括住。
②值1,值2,值3,…,值N必须与switch表达式的值具有相同的数据类型,且都是常量表达式,不能出现变量:1+x
③当switch表达式的值与case语句的值想匹配时,执行从该语句开始,直到遇到break语句,或到达该switch语句的结束。
④如果给出的case语句的值没有与switch表达式相匹配时,执行默认语句default;
⑤break,表示退出选择。 -
警告
不要忘记在需要的时候使用break语句,一旦匹配其中一个case,就从匹配的case处开始执行,直到遇到break语句或者到达switch语句的结束。 -
扩充
-
break:只能用在循环或switch语句中
在循环中表示跳出当前的循环(最近的那循环)
在switch语句中表示退出选择 -
continue:只能在循环语句中使用
在循环中表示跳过当前循环,开启下一轮循环
import java.util.Scanner;
class Demo09{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("1.PHP是世界上最好的语言");
System.out.println("A.呵呵");
System.out.println("B.嘻嘻");
System.out.println("C.啦啦");
System.out.println("D.哈哈");
System.out.print("请选择:");
String choice=scanner.nextLine();
//byte short int char boolean String 枚举
//或者是以上数据类型的数组当中的某一项
switch(choice){
case "A":
System.out.println("你错了!呵呵");
//break;
//如果没有break,选择A的话,"你错了!呵呵""你错了!嘻嘻"同时出现
case "B":
System.out.println("你错了!嘻嘻");
break;
case "C":
System.out.println("你错了!啦啦");
break;
case "D":
System.out.println("你错了!哈哈");
break;
default:
System.out.println("不存在!沙雕");
break;
}
/*
switch只能对单一的值进行判断 不能判断区间值
if可以对单一值进行判读也可以区间值
*/
}
}
import java.util.Scanner;
class Demo10{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.print("Enter a year: ");
int year=input.nextInt();
switch(year%12){
case 0: System.out.println("monkey");break;
case 1: System.out.println("rooster");break;
case 2: System.out.println("dog");break;
case 3: System.out.println("pig");break;
case 4: System.out.println("rat");break;
case 5: System.out.println("ox");break;
case 6: System.out.println("tiger");break;
case 7: System.out.println("rabbit");break;
case 8: System.out.println("dragon");break;
case 9: System.out.println("snake");break;
case 10:System.out.println("horse");break;
case 11:System.out.println("sheep");break;
}
}
}