四、Java的三种基本语句

目        录

(一)判断语句

(一)if语句

(1)概念

(2)格式

(3)代码测试

(二)if-else语句

(1)概念

(2)格式

(3)代码测试

(三)if-else-else if 语句

(1)概念

(2)格式

(3)代码测试

(二)选择语句

(一)switch

(1)概念

(2)格式

(3)代码测试

(三)循环语句

(一)for循环

(1)概念

(2)格式

(3)代码测试

(二)while循环

(1)概念

(2)格式

(3)代码测试

(三)do...while 循环

(1)概念

(2)格式

(3)代码测试

(四)扩展

(一)for和while的区别

(二)跳出语句

(1)break

(2)continue

(三)死循环

(四)for嵌套循环


(一)判断语句

(一)if语句

(1)概念

if语句属于分支结构,会根据对表达式的判断选择对应的执行语句或者条件。

(2)格式

image-20210804200904199

(3)代码测试

package cn.tedu.exercise;
​
import java.util.Scanner;
​
public class biji {
    public static void main(String[] args) {
        System.out.println("请输入一个整数:");
        int number=new Scanner(System.in).nextInt();
        if(number%2==0){
            System.out.println(number+"能被2整除");
        }
    }
}

(二)if-else语句

(1)概念

多分支结构,对满足的判断结果有对应的执行结果,而不满足判断语句的也有相应的语句。

(2)格式

image-20210804201727546

(3)代码测试

package cn.tedu.exercise;
​
import java.util.Scanner;
​
public class biji {
    public static void main(String[] args) {
        System.out.println("请输入一个整数:");
        int number=new Scanner(System.in).nextInt();
        if(number%2==0){
            System.out.println(number+"能被2整除");
        }else {
            System.out.println(number+"不能被2整除");
        }
    }
}

(三)if-else-else if 语句

(1)概念

嵌套循环,判断条件有多种情况(大于两种)。

(2)格式

image-20210804202217194

(3)代码测试

package cn.tedu.basic;
​
import java.util.Scanner;
​
public class TestScore {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("请输入你的成绩:");
        Double score=new Scanner(System.in).nextDouble();
        if(score<0||score>100) {
            System.out.println("输入的数据不正确!");
        }else
        {
            if(score>=90){
                System.out.println("优秀");
            }else if(score>=80) {
                System.out.println("良好");
            }else if(score>=70) {
                System.out.println("中等");
            }else if(score>=60) {
                System.out.println("及格");
            }else {
                System.out.println("不及格");
            }
            /*
             * 为了增强程序的健壮性,我们可以对分数进行判断
             * 如果是合理范围内的分数,就判断段位不合理,结束程序
             */
        }
    }
}

(二)选择语句

(一)switch

(1)概念

switch case 语句用来判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。当一个case成立,从这个case向后穿透所有case,包括default,直到程序结束或者遇到break程序才结束。

(2)格式

image-20210804203527460

(3)代码测试

package cn.tedu.basic;
​
public class TestSwitch2 {
​
    public static void main(String[] args) {
        String s="5";
        switch(s) {
            case "1" :System.out.println("吃冒菜");break;
            case "2" :System.out.println("吃干锅");break;
            case "3" :System.out.println("吃烧烤");break;
            case "4" :System.out.println("吃麻辣烫");break;
            case "5" :System.out.println("吃火锅");break;
            case "6" :System.out.println("吃小龙虾");break;
            case "7" :System.out.println("吃饭");break;
            default:System.out.println("不可能");
        }
    }
}

(三)循环语句

(一)for循环

(1)概念

循环结构是指在程序中需要反复执行某个功能而设置的一种程序结构。它由循环体中的条件,判断继续执行某个功能还是退出循环。根据判断条件,循环结构又可细分为先判断后执行的循环结构和先执行后判断的循环结构。

(2)格式

image-20210804203729762

(3)代码测试

package cn.tedu.basic;
​
public class TestFor {
​
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for(int i=1;i<=100;i++) {
            System.out.println("又不是没那条件,干就完啦!");
        }
        for(int i=1;i<=10;i++) {
            System.out.println(i);
        }
    }
}

(二)while循环

(1)概念

同for()循环,while循环先判断后执行。

(2)格式

image-20210804204227268

(3)代码测试

package cn.tedu.basic1;
​
import java.util.Random;
import java.util.Scanner;
/*
 * 本类用于练习while结构
 * 需求:生成一个随机数让用户猜
 */
public class TestWhile {
    public static void main(String[] args) {
        int random=new Random().nextInt(100);
        //参数100是自定义的,此时生成的随机数范围是[0,100)以内的整数
        while(true) {//死循环
            //注意:死循环一定要设置出口
            System.out.println("请输入您要输入的整数:");
            int input =new Scanner(System.in).nextInt();
            if(input==random) {
                System.out.println("恭喜你!你猜对啦!");break;
            }else if (input>random) {
                System.out.println("猜大啦!");
            }else if (input<random) {
                System.out.println("猜小啦!");
            }
        }
    }
}

(三)do...while 循环

(1)概念

同for循环,但与while有区别,do...while先执行后判断。

(2)格式

image-20210804205040476

(3)代码测试

package cn.tedu.basic1;
​
import java.util.Scanner;
​
public class TestDowhile {
​
    public static void main(String[] args) {
        int random=new Scanner(System.in).nextInt(100);
        do {
            System.out.println("猜猜看");
            int input=new Scanner(System.in).nextInt();
            if (input>random) {
                System.out.println("猜大啦!");
            }else if (input<random) {
                System.out.println("猜小啦!");
            }else if(input==random) {
                System.out.println("恭喜你!你猜对啦!");break;}
        }while(true);
    }
}

(四)扩展

(一)for和while的区别

①for循环结束后,就无法再使用,而while循环结束后还可以继续使用,因为for循环结束后,该变量就会从内存中消失,能提高内存的使用效率。

②在已知循环次数的时候推荐使用for,循环未知的时候推荐使用while。

(二)跳出语句

(1)break

①概述:直接跳出循环,不执行循环语句里的语句,但要执行循环体外面的语句。

②格式:

switch(i){
    case (..):...break;
    case (..):...break;
    default:.......break;
        }

③代码测试:

package cn.tedu.basic;
​
public class TestSwitch {
    
    public static void main(String[] args) {
        int a =7;
        switch(a) {
            case 1:System.out.println(1);break;
            case 2:System.out.println(2);break;
            case 3:System.out.println(3);break;
            case 4:System.out.println(4);break;
            case 5:System.out.println(5);break;
            default:System.out.println(0);
        }
    }
}

(2)continue

①概述:直跳出本次循环,不执行本次循环语句,但会继续进行下一轮循环。

②格式:

  int a =7;
       switch(a) {
           case 1:;break;
           case 2:;break;
           case 3:;continue;
           case 4:;break;
           case 5:;break;
           default:;
      }

③代码测试:

package cn.tedu.basic1;
​
import java.util.Scanner;
​
public class TestBreakAndContinue {
​
    public static void main(String[] args) {
        for(int i=1;i<=100;i++) {
            System.out.println("请输入您要猜的整数:");
            int input =new Scanner(System.in).nextInt();
            if(input!=88) 
                continue;
            if(input==88) {
                System.out.println("恭喜你!猜对了!");
                break;
            }
        }
    }
}

(三)死循环

①while判断语句,当循环条件为true或者为1时,该循环为死循环。

②for循环语句,当格式为:for(;;)时,可视为死循环。

(四)for嵌套循环

①外循环加内循环,外层循环执行一次,内层循环执行n次。

②格式:

for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
        
    }
}

③代码测试:

package cn.tedu.basic;
​
public class TestForDemo {
​
    public static void main(String[] args) {
        /*
         * 外层循环控制的是行数,内层循环控制的是一行的列数
         */
        for(int i=1;i<=9;i++) {
            for(int j=1;j<=i;j++) {
                System.out.print(j+"x"+i+"="+i*j+"\t");
            }
            System.out.println();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值