【JAVA基础学习笔记5】

本文介绍了Java中Scanner类的基本用法,包括next()和nextLine()的区别,以及如何处理不同数据类型。同时,讲解了if分支结构和switch分支的选择与执行特点。此外,还探讨了while和do-while循环的差异,并通过实例展示了for循环在打印乘法表和三角形中的应用。

用户交互Scanner

  • next()方法
  • nextLine()方法
public static void main(String[] args) {
        //创建一个扫描对象,用于接收键盘数据
       Scanner scanner = new Scanner(System.in);
       //使用next方法输入  只会输入空格之前的内容
       String str0 = scanner.next();//hello world
       System.out.println("使用next输入"+str0);// hello
       //使用nextline方法输入,会输入回车之前的内容
       String str1 = scanner.nextLine();//hello world
       System.out.println("使用nextline输入"+str1);//hello world
       scanner.close();
  • 这两种方法中还包括具体的数据类型(int、byte…)
 public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //求输入的多个数据的的平均值,数据之间以回车隔开
        double sum = 0;
        int m = 0;
        while (scanner.hasNextDouble()){
            double x = scanner.nextDouble();//double 类型
            sum = sum + x;
            m++;
            System.out.println("当前和为:"+sum);
            System.out.println("当前个数为:"+m);
            System.out.println("当前平均数为:"+sum/m);
        }
        scanner.close();

If分支结构

  • 先判断再执行
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        if(score==100){
            System.out.println("满分");
        }else if(score<100 && score>=80){
            System.out.println("优秀");
        }else if(score<80 && score>=70){
            System.out.println("良好");
        }else if(score<70 && score>=60){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }
        scanner.close();
    }
  • 一旦满足某一条件,后面的分支将不再执行

switch分支

  • switch case判断一个变量与一系列值中的某个值是否相等
  • 从Java SE 7开始支持字符串类型匹配
  • case必须为字符常量或字面量
  • break使用时,如果某一分支结束不添加break,会发生穿透现象,添加则推出循环
  • continue使用时,添加continue推出某一次循环,跳过某种状态
  • 使用字符串时,本质上还是使用的数字,具体可以通过反编译验证
 public static void main(String[] args) {
        char grade = 'C';
        //case穿透,switch 匹配一个具体的值
        switch (grade){
            case 'A':
                System.out.println("A");
                break;
            case 'B':
                System.out.println("B");
                break;
            case 'C':
                System.out.println("C");
                break;
        }
        //switch 从7之后支持string类型
        //反编译 将class文件复制到IDEA的java文件夹中打开,可以看到  字符串匹配时采用的是hashcode  还是数字
    }

While 、Do While

  • while时先判断,再执行
  • do while 是先执行再判断
  • 若判断条件永远为ture,则为死循环
 public static void main(String[] args) {
        int a = 4;
        int b = 4;
        while (a<4){
            a++;
            System.out.println(a);
        }//空
        System.out.println("===================================");
        do{
            b++;
            System.out.println(b);
        }while (b<4);//4
    }

For循环

  • 最先执行变量初始化,然后判断布尔值,最后更新变量
  • 三个参数都可省略`for( ; ; )为死循环
  • 用法灵活多样
  • 给出一个输出乘法表格的例子:
public static void main(String[] args) {
        // 打印九九乘法表
        for (int j = 1; j <=9; j++) {
            for (int i = 1; i <=j; i++) {
                System.out.print(i+"*"+j+"="+(i*j)+'\t');
            }
            System.out.println();
        }
    }
  • 再给出一个输出三角形的例子:
 public static void main(String[] args) {
        //打印三角形
        for(int i=1;i<=5;i++){
            for(int j=5;j>=i;j--){
                System.out.print(" "+'\t');
            }
            for(int j=1;j<=i;j++){
                System.out.print("*"+'\t');
            }
            for(int j=1;j<i;j++){
                System.out.print("*"+'\t');
            }
            System.out.println();
        }
    }
    //println 输出完自动换行
     //print 输出完不会自动换行
 	 	 	 	 	*	
 	 	 	 	*	*	*	
 	 	 	*	*	*	*	*	
 	 	*	*	*	*	*	*	*	
 	*	*	*	*	*	*	*	*	*	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值