正常代码运行的顺序,从上到下。
案例:
public static void cd1(){
int a = 10;
int b = 20;
System.out.println(a-b);
}
(1)if分支结构:
定义:根据条件的真或假,来决定执行某段代码。
格式:
if..; if..else..; if..else if ..else;
案例:
public static void cd1(){
int a = 10;
int b = 20;
if(a>b){
System.out.println("a大于b");
}
if(a>b){
System.out.println("a大于b");
}else{
System.out.println("a不大于b");
}
if(a>b){
System.out.println("a大于b");
}else if(a==b){
System.out.println("a等于b");
}
}
(2)switch分支结构:
定义:是通过比较值是否相等,来决定执行那条分支。
格式:
* switch (变量) {
case 值1:
// 执行语句块1
break;
case 值2:
// 执行语句块2
break;
...
default:
// 默认执行语句块
}
案例:
public static void cd2(){
System.out.println("请输入红绿灯的状态:");
Scanner sc = new Scanner(System.in);
String status = sc.next();
switch (status){
case "red":
System.out.println("停止");
break;
case "yellow":
System.out.println("Caution");
break;
case "green":
System.out.println("Go");
break;
default:
System.out.println("输入错误");
}
}
if、switch的比较:各自适合什么业务场景:
注意:
请使用运行主函数调用:
public static void main(String[] args) {
cd1();
cd2();
}
(1)for循环
定义:控制一段代码反复执行很多次。
格式:
for (初始化语句; 条件判断语句; 循环后的操作语句) {
循环体
}
案例:
//一个for循环,打印三次hello
public static void main1() {
for (int i = 0; i < 3; i++) {
System.out.println("hello");
}
}
(2)while循环:
定义:控制一段代码反复执行很多次。
格式:
while (条件判断语句) {
循环体
}
案例:
public static void main2() {
int i = 0;
while (i < 3) {
System.out.println("hello");
i++;
}
}
什么时候用for,什么时候用while?
(3)do…while循环:
定义:控制一段代码反复执行很多次。
格式:
do {
循环体
} while (条件判断语句);*/
案例:
public static void main3() {
int i = 0;
do {
System.out.println("hello");
i++;
} while (i < 3);
}
小结:
定义:一直在运行,出不来。
案例:
//请给我用while,for,dowhile实现的死循环
public static void main4() {
while (true) {
System.out.println("hello");
}
}
public static void main5() {
for (; ; ) {
System.out.println("hello");
}
}
public static void main6() {
do {
System.out.println("hello");
} while (true);
}
定义:一个循环里面还有循环
案例:
//打印一个3*3的*
public static void main7() {
for (int i=0;i<3;i++){
for (int j=0;j<3;j++){
System.out.println("*");
}
}
}
//打印九九乘法表
public static void main8() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\t");
}
System.out.println();
}
}
(1)break:
定义:跳出并结束当前所在循环的执行。
(2)continue
定义:用于跳出当前循环的当前执行,直接进入循环的下一次执行。只能在循环中使用。
案例:
public static void main9() {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);//0,1,2,3,4
}
for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
System.out.println(i);//0,1,2,3,4,6,7,8,9
}
}
案例1:
//综合案例简单计算器
public static void main10() {
Scanner in=new Scanner(System.in);
System.out.println("请输入第一个数字");
double a=in.nextDouble();
System.out.println("请输入第二个数字");
double b=in.nextDouble();
System.out.println("请输入运算符(+,-,/,*)");
String c=in.next();
switch (c){
case "+":
System.out.println(a+b);
break;
case "-":
System.out.println(a-b);
break;
case "*":
System.out.println(a*b);
break;
case "/":
System.out.println(a/b);
break;
default:
System.out.println("输入错误");
}
}
综合案例2:
//猜数字游戏
public static void main11() {
Scanner in=new Scanner(System.in);
int a=(int)(Math.random()*100)+1;//[0,99]+1=>[1,100]
/*Random r=new Random()
* int x=r.nextInt(100)+1
* 随机生成的不同方式*/
while (true){
System.out.println("请输入数字");
int b=in.nextInt();
if (a==b){
System.out.println("恭喜你猜对了");
break;
}
if (a>b){
System.out.println("猜小了");
}else {
System.out.println("猜大了");
}
}
}
综合案例3:
//验证码:开发一个程序,可以生成指定位数的验证码,每位都可以是数字,大小写字母
public static void main12() {
String code="";
Scanner in=new Scanner(System.in);
System.out.println("请输入验证码位数");
int n=in.nextInt();
for (int i = 0; i < n; i++) {
int a=(int)(Math.random()*3);
//使用switch判断当前位置的字符类型
switch (a){
case 0:
code+=(char)(Math.random()*26+'a');
break;
case 1:
code+=(char)(Math.random()*26+'A');
break;
case 2:
code+=(int)(Math.random()*10);
break;
}
}
System.out.println(code);
}
}
提醒:请使用运行主函数调用。