学习笔记-02Java基础-流程控制

用户交互Scanner

Scanner基础

  • 通过Scanner类来获取用户的输入;

  • 基本语法:

    Scanner s =new Scanner(System.in)

  • 通过Scanner类的next()与nextLine()方法获取输入的字符串;

    next()

    1. 一定要读取到有效字符后才可以结束输入;
    2. 对输入有效字符之前遇到的空白,next()方法会自动将其去掉;
    3. 只有输入有效字符后才将其后面输入的空白作为分隔符或结束符;
    4. next()不能得到带有空格的字符串;

    nextLine()

    1. 以Enter作为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符;
    2. 可以获得空白;
  • 获取之前需要用hasNext()和hasNextLine()判断是否还有输入的数据;

  • 判断是否还有输入的数据hasNext()hasNextLine()
    获取输入的字符串next()nextLine()

使用next方法:

package com.wang.www.scanner;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //1.创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);  //new Scanner(System.in);
        System.out.println("使用next方式接收数据");

        //2.判断用户有没有输入字符串
        if (scanner.hasNext()){
            //3.使用next方式接收
            String str=scanner.next();//程序会等待输入完毕
            System.out.println("输出的内容为:"+str);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关闭
        scanner.close();

    }
}

使用nextLine()方法:

package com.wang.www.scanner;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        if (scanner.hasNextLine()){
            String str=scanner.nextLine();
            System.out.println(str);
        }
    }
}

Scanner进阶使用

判断输入数据是否为整数

package com.wang.www.scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //从键盘接收数据
        int i = 0;
        float f = 0.0f;
        System.out.println("请输入整数:");
        //如果...那么
        if (scanner.hasNextInt()) {
            i = scanner.nextInt();
            System.out.println("整数数据" + i);
        } else {
            System.out.println("输入的数据不是整数数据");
        }

        scanner.close();
    }}

输入多个数字,并求其总和和平均数,每输入一个数字按回车确认,通过输入非数字来结束并输出执行结果;

package com.wang.www.scanner;
import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //定义好初始变量
        double sum=0;
        int i=0;
        System.out.println("请输入数字");

        //通过循环去判断是否还在输入,并在里面对每一次输入的数据进行求和和统计
        while (scanner.hasNextDouble()){
            double x=scanner.nextDouble();
            sum=sum+x;
            i=i+1;
            System.out.println("你输入了第"+i+"个数据,当前结果sum="+sum);
        }
        System.out.println(i+"个数的和为"+sum);
        System.out.println(i+"个数的平均数为"+(sum/i));



    }


}

02顺序结构

  • JAVA基本结构就是顺序结构,除非特别指明,否则按照顺序一句一句执行;
  • 顺序结构是最简单的算法结构;
  • 顺序结构是任何一个算法都离不开的基本算法结构;

03*选择结构

if选择结构

if单选择结构

语法:

if(布尔表达式){

//如果布尔表达式为true将执行的语句

}

public class Demo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容:");
        String s=scanner.nextLine();
        //equals:判断字符串是否相等
        if (s.equals("hello")){
            System.out.println(s);
        }
        System.out.println("End");
        scanner.close();
if双选择结构

语法:

if(布尔表达式){

//如果布尔表达式为true将执行的语句

}else{

////如果布尔表达式为false将执行的语句

}

public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score=scanner.nextInt();
        if (score>60){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }
        scanner.close();
    }
}
if多选择结构

语法:

if(布尔表达式1){

//如果布尔表达式1为true将执行的语句

}else if(布尔表达式2){

////如果布尔表达式2为true将执行的语句

}else if(布尔表达式3){

////如果布尔表达式3为true将执行的语句

}else{

////如果以上布尔表达式都不为true将执行的语句

}

注:

  • if语句可有多个else if语句,必须在else之前;
  • 一旦一个else if语句检测为true,其他的else if以及else都将跳过执行
  • if语句至多有一个else语句,else语句在所有的else if语句之后;
import java.util.Scanner;

public class Demo04 {
    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>=90&&score<100){
            System.out.println("A级");
        }else if (score>=80&&score<90){
            System.out.println("B级");
        }else if (score>=70&&score<80){
            System.out.println("C级");
        }else if (score>=60&&score<70) {
            System.out.println("D级");
        }else {
            System.out.println("不及格");
        }

        scanner.close();
    }
}
嵌套的if结构

if语句或else if语句中嵌套if语句或else if 语句;

Switch选择结构

  • 多选择还有一个实现方式即switch case语句;

  • switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支

  • switch case语句变量类型可以是:

    1. byte、short、int、char

    2. 从JavaSE7开始,支持字符串String类型;同时case标签必须为字符串常量或字面量;

      基本语法:

      //基本语法
      switch (expression){
          case value:
              //语句
              break;//可选:每一个case都可加break阻止case穿透
              //break:表示执行此case后不输出后面的case值
          case value:
              //语句
              break;
          default://语句
      }
      
    public class Demo01 {
        public static void main(String[] args) {
            char grade='B';
            //switch匹配一个具体的值
            //case穿透
            switch (grade){
                case 'A':
                    System.out.println("优秀");
                    break;
                case 'B':
                    System.out.println("良好");
                    break;
                case 'C':
                    System.out.println("及格");
                case 'D':
                    System.out.println("不及格");
                default:
                    System.out.println("输入错误");
    
            }
        }
    }
    

04*循环结构

While循环

在这里插入图片描述

package com.wang.www.processControl.while01;

public class whileDemo01 {
    public static void main(String[] args) {
        //输出1-100
        int i=0;
        while (i<100){
             i++;
            System.out.println(i);
        }
        //死循环
        while (true){
            //等待客户端连接
            //定时检查
        }
    }
}
public class whileDemo02 {
    public static void main(String[] args) {
        //计算1+2+3+..+100=?
        int i=0;
        int sum=0;
        while (i<100){
            i=i+1;
            sum=sum+i;
            System.out.println(i);
            System.out.println(sum);
        }
        System.out.println("和为"+sum);
    }
}

DoWhile循环

在这里插入图片描述

public class DoWhileDemo02 {
    public static void main(String[] args) {
        //输出1-100
        int a=0;
        do {
            a++;
            System.out.println(a);
        }while (a<100);
    }
}
public class DoWhileDemo01 {
    public static void main(String[] args) {
        //计算1+2+3+..+100=?
        int i=0;
        int sum=0;
        do {
            i=i+1;
            sum=sum+i;
        }while (i<100);
        System.out.println(sum);

    }
}
public class demo03 {
    public static void main(String[] args) {
        //While&DoWhile比较
        //While:先判断后执行;
        //DoWhile:先执行后判断
        int i=0;
        while (i<0){
            i++;
        }
        System.out.println(i);//输出结果为0:判断<0不成立,不执行i++
        System.out.println("======================================================");
        do {
            i++;
        }while (i<0);
        System.out.println(i);//输出结果为1:先执行i++,再判断<0不成立
    }
}

*For循环

100.f -idea快捷键
在这里插入图片描述

注:

  1. 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,可以是空语句
  2. 然后,检测布尔表达式的值。如果为true,循环体执行;false,循环终止。开始执行循环体后语句;
  3. 再次检测布尔表达式。循环执行上面的过程;
练习
01输出0-100之间奇数和偶数的和
public class for01 {
    public static void main(String[] args) {
        //输出0-100之间奇数和偶数的和
        int oddsum=0;//奇数
        int evensum=0;//偶数
        for (int i = 0; i <=100; i++) {
            if (i%2!=0) {//奇数
                oddsum=oddsum+i;
            }else {//偶数
                evensum=evensum+i;
            }
        }
        System.out.println("奇数和为:"+oddsum);
        System.out.println("偶数和为:"+evensum);
    }

}
02用while或for循环输出1-1000之间能被5整除的数,并且每行输出三个
public class for02 {
    public static void main(String[] args) {
        //用while或for循环输出1-1000之间能被5整除的数,并且每行输出三个
        for (int i = 0; i <= 1000; i++) {
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.println( );
                // system.out.print("\n");
            }
            //println输出后会换行
            //print输出后不会换行
        }
    }
}
03打印九九乘法表
package com.wang.www.processControl.while01;

public class for03 {
    public static void main(String[] args) {
        /*
        1*1=1  
        2*1=2  2*2=4  
        3*1=3  3*2=6  3*3=9  
        4*1=4  4*2=8  4*3=12 4*4=16 
        5*1=5  5*2=10 5*3=15 5*4=20 5*5=25 
        6*1=6  6*2=12 6*3=18 6*4=24 6*5=30 6*6=36 
        7*1=7  7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49 
        8*1=8  8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64 
        9*1=9  9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81 
         */
        //嵌套for循环
        //1.打印第一列
        //2.把固定的1再用一个循环体包起来
        //3.去掉重复项,i<=j
        //4.调整样式
        for (int j =1; j <= 9; j++) {
            for (int i = 1; i <=j; i++) {
                System.out.print(j+"*"+i+"="+(j*i)+"\t");
        }
            System.out.println();
        }

        }
    }
增强for循环

在这里插入图片描述

public class for05 {
    public static void main(String[] args) {
        int[] numbers={10,20,30,40,50};//定义了一个数组
        //1.for循环写法
        for (int i = 0; i < 5; i++) {
            System.out.println(numbers[i]);
        }
        System.out.println("=====================================");
        //1.增强for循环写法
        for (int x:numbers){
            System.out.println(x);
        }
    }
}

05break、continue、goto

在这里插入图片描述

break:强制退出;

continue:跳过;(用于终止某次循环过程,即跳过循环体尚未执行的语句,接着进行下一次是否执行循环的判定)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值