Java流程控制

(一)用户交互Scanner

我们可以通过Scanner类来获取用户的输入。

基本语法:

Scanner s=new Scanner(System.in);

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

在读取前我们一般需要使用hasNnext()与hasNextLine()判断是否还有输入的数据。

public class Demo01 {
    public static void main(String[] args) {
        //创建一个扫描对象,用于接收键盘数据
        Scanner scanner=new Scanner(System.in);
        //判断用户有没有输入字符串
        System.out.println("使用next方式接收:");
        if(scanner.hasNext()){
            //使用next方式接收
            String str1=scanner.next();
            System.out.println("输入的内容为:"+str1);
        }
        //凡是属于IO流的类如果不关闭,会一直占用资源,要养成良好习惯,用完就关闭
        scanner.close();
    }
}
public class Demo02 {
    public static void main(String[] args) {
        //创建一个扫描对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        //判断用户有没有输入字符串
        System.out.println("使用nextLine方式接收:");
        if (scanner.hasNextLine()){
            //使用nextLine方式接收
            String str2 = scanner.nextLine();
            System.out.println("输入的内容为:" + str2);
        }
        scanner.close();
    }
}

next():

  1. 一定要得到有效字符后才可以结束输入

  2. 对输入有效字符之前遇到的空白,next()方法会自动将其去掉

  3. 只有输入有效字符后才将其后面输入的空白作为分隔符或结束符

  4. next()不能得到带有空格的字符串

nextLine():

  1. 以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符

  2. 可以获得空白

以上两组的程序运行结果

进阶使用

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("输入的不是整数数据");
        }
//----------------------------------------------------
        System.out.println("请输入小数:");
        if(scanner.hasNextFloat()){
            f=scanner.nextFloat();
            System.out.println("小数数据:"+f);
        }
        else{
            System.out.println("输入的不是小数数据");
        }
        scanner.close();
    }
}

运行结果

public class Demo04 {
    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();
            m=m+1; //m++
            sum=sum+x;
            System.out.println("输入第"+m+"个数据,当前结果sum="+sum);
        }
        System.out.println(m+"个数的和为"+sum);
        System.out.println(m+"个数的平均值为"+(sum/m));
        scanner.close();
    }
}

(二)顺序结构

按照从上到下的顺序依次执行

(三)选择结构

if选择结构

if单选择结构

if(布尔表达式){
    //如果布尔表达式为true将执行的语句
}
public class IfDemo01 {
    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{
    //如果布尔表达式为Fslse将执行的语句
}
public class IfDemo02 {
    public static void main(String[] args) {
        //考试分数大于60就是及格,小于60就是不及格
        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将执行的语句
}
public class IfDemo03 {
    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>=90){
            System.out.println("A");
        }else if(score<90&&score>=80){
            System.out.println("B");
        }else if(score<80&&score>=70){
            System.out.println("C");
        }else if(score<70&&score>=60){
            System.out.println("D");
        }else if(score<60&&score>=0) {
            System.out.println("不及格");
        }else{
            System.out.println("输入成绩不合法");
        }
        scanner.close();
    }
}

嵌套的if结构

if(布尔表达式 1){
    //如果布尔表达式1为true将执行的语句
    if(布尔表达式 2){
    //如果布尔表达式2为true将执行的语句
    }
}

switch选择结构

switch(expression){
    case value:
        //语句
        break;//可选
    case value:
        //语句
        break;//可选
    //可以有任意数量的case语句
    default://可选
        //语句
}

switch语句中的变量类型可以是:

  1. byte、short、int或char

  2. 从Java SE 7开始,switch支持字符串String类型了,同时case标签必须为字符串常量或字面量

public class SwitchDemo01 {
    public static void main(String[] args) {
        char grade='F';
        //switch匹配一个具体的值
        switch(grade){
            //case穿透
            case'A':
                System.out.println("优秀");
                break;//可选
            case'B':
                System.out.println("良好");
                break;//可选
            case'C':
                System.out.println("中等");
                break;//可选
            case'D':
                System.out.println("及格");
                break;//可选
            case'E':
                System.out.println("不及格");
                break;//可选
            default:
                System.out.println("未知等级");
        }
    }
}
public class SwitchDemo02 {
    public static void main(String[] args) {
        String name="Joyce_yu";
        //JDK7的新特性,表达式结果可以是字符串!
        //字符的本质还是数字
        //反编译 java---class(字节码文件)---反编译(IDEA)
        switch (name){
            case"赵":
                System.out.println("赵.");
                break;
            case"钱":
                System.out.println("钱.");
                break;
            case"孙":
                System.out.println("孙.");
                break;
            case"Joyce_yu":
                System.out.println("Joyce_yu.");
                break;
            default:
                System.out.println("???");
        }
    }
}

(四)循环结构

while循环

while(布尔表达式){
    //循环内容
}
//只要布尔表达式为true,循环就会一直执行下去

do...while循环

do{
    //代码语句
}while(布尔表达式);

while和do...while的区别:

  • while是先判断后执行,do...while是先执行后判断

  • do...while循环总是保证循环体会被至少执行一次!(这是两者的主要差别)

public class WhileDemo{
    public static void main(String[] args) {
        int a=0;
        while (a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("==========================");
        do {
            System.out.println(a);
            a++;
        }while(a<0);
    }
}

for循环

for(初始化;布尔表达式;更新){
    //代码语句
}
public class ForDemo01 {
    public static void main(String[] args) {
        int a=1;  //初始化条件
        while(a<=100){  //条件判断
            System.out.println(a);  //循环体
            a+=2;  //迭代
        }
        System.out.println("while循环结束!");
        //初始化 //条件判断 //迭代
        for (int i=1;i<=100;i++){
            System.out.println(i);
        }
        System.out.println("for循环结束!");
    }
}
public class ForDemo02 {
    public static void main(String[] args) {
        //输出1-1000之间能被5整除的整数,并且每行输出3个
        for (int i = 0; i < 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){  //每行
                System.out.println();
            }
        }
      //println输出完会换行
      //print输出完不会换行
      //System.out.println();==System.out.print("\n");
    }
}
public class ForDemo03 {
    public static void main(String[] args) {
         /* 打印九九乘法表
           ①先打印第一列
           ②把固定的1再用一个循环包起来
           ③去掉重复项,i<=j
           ④调整样式 
        */
        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循环

for(声明语句:表达式)
{
    //代码语句
}
/*
  声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。
          其作用域限定在循环语句块,其值与此时数组元素的值相等。
  表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
*/
public class ForDemo04 {
    public static void main(String[] args) {
        int[] numbers={10,20,30,40,50};   //定义了一个数组
        for (int i = 0; i < 5; i++) {
            System.out.println(numbers[i]);
        }
        System.out.println("============================");
        //遍历数组的元素
        for (int x:numbers);{
            System.out.println(x);
        }
    }
}

(五)break与continue

  • break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)

  • continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秃头酱酱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值