Java控制流程

目录

1.用户交互Scanner

2.if选择结构

3.switch多选择结构

4.while循环

5.do while 循环

6. For循环

7.break & continue

8.练习


1.用户交互Scanner

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

public class Demo02 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in) ;
        //从键盘接收数据
        //nextLine方式接收数据
        System.out.println(("nextLine方式接收:"));
        //判断是否还有输出
        if(scan.hasNextLine()){
            String str2 = scan.nextLine();
            System.out.println("输出内容"+str2);
        }
        scan.close();
    }
}

两者区别:
next():
①一定要读取到有效字符后才可以结束输入。
②对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。
③只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
④next() 不能得到带有空格的字符串。
nextLine():
①以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。
②可以获得空白。

scanner其他方法

public class Demo03 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //从键盘接收数据
        int i = 0;
        float f = 0.0f;
        System.out.print("输入整数: ");
        if (scan.hasNextInt()){
            //判断输入的是否是整数
            i = scan.nextInt();
            //接收整数
            System.out.println("整数数据:"+i);
        }else{
            //输入错误的信息
            System.out.println("输入的不是整数");
        }
        System.out.print("输入小数:");
        if(scan.hasNextFloat()){
            f = scan.nextFloat();
            System.out.println("小数数据:"+f);
        }else{
            System.out.println("输入的不是小数!");
        }
        scan.close();

    }
}

2.if选择结构

使用 if,else if,else 语句的时候,需要注意下面几点:
if 语句至多有 1 个 else 语句,else 语句在所有的 else if 语句之后。
if 语句可以有若干个 else if 语句,它们必须在 else 语句之前。
一旦其中一个 else if 语句检测为 true,其他的 else if 以及 else 语句都将跳过执行。

public class Demo05 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //接收用户数据
        System.out.print("请输入内容:");
        String  s = scanner.nextLine();

        if (s.equals("Hello")){
            System.out.println("输入的是:"+ s);

        }
        System.out.println("End" );
        scanner.close();
public class Demo06 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入成绩:");
        int score = scanner.nextInt();

        if (score >= 60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }
        scanner.close();
    }
}
public class Demo07if {
    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  {
            System.out.println("输入的成绩不合法!");
        }
       scanner.close();

    }
}

3.switch多选择结构

switch case 语句有如下规则:

①switch 语句中的变量类型可以是: byte、short、int 或 char。从 Java SE 7 开始,switch 支持
字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。

②switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。

③case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。

④当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现,才会跳出 switch 语句。

⑤当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现
break 语句。

⑥switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何
位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分
支不需要 break 语句。

⑦switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断是否继续输出,或是跳出判断。

public class Demo08switch {
    public static void main(String[] args) {
        char grade = 'C';
        switch (grade)
        {
            case'A':
                System.out.println("优秀");
                break;
            case'B':
                System.out.println("良好");
                break;
            case'C':
                System.out.println("一般");
               // break;
            default:
                System.out.println("未知等级");
        }
        System.out.println("你的等级是" + grade);
    }

}

4.while循环

public class Demo09while {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        while (i <= 100){
            sum = sum +i;
            i++;

        }
        System.out.println("Sum=" + sum);
    }
}

5.do while 循环

public class Demo10dowhile {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do{
            sum = sum + i;
            i++;
        }while(i <= 100);
        System.out.println("Sum=" + sum);
    }

}

While和do-While的区别:
①while先判断后执行。dowhile是先执行后判断!
②Do...while总是保证循环体会被至少执行一次!这是他们的主要差别

6. For循环

关于 for 循环有以下几点说明:
①最先执行初始化步骤。可以声明一种类型,可初始化一或多个循环控制变量,也可以是空语句。
②然后,检测布尔表达式的值。如果为 true,循环体被执行。如果为false,循环终止,开始执行循
环体后面的语句。
③执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
④再次检测布尔表达式。循环执行上面的过程

public class Demo11for {
    public static void main(String[] args) {
        int a = 1;
        while(a<=100){
            System.out.println(a);
            a+=2;
        }
        System.out.println("while循环结束!");
        System.out.println("=================");
        for (int i =1 ;i<=100;i++){
            System.out.println(i);
        }
        System.out.println("for循环结束!");
    }
}
public class Demo15For {
    public static void main(String[] args) {
        int [] numbers = {10,20,30,40,50};
        for(int x: numbers){
            System.out.print(x);
            System.out.print(",");
        }
    }
}

7.break & continue

break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。
break 跳出最里层的循环,并且继续执行该循环下面的语句

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
在 for 循环中,continue 语句使程序立即跳转到更新语句。
在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句

public class Demo16break {
    public static void main(String[] args) {
        int i = 0;
        while(i < 100){
            i++;
            System.out.println(i);
            if(i==30){
                break;
            }
        }
    }
}
public class Demo17continue {
    public static void main(String[] args) {
        int i=10;
        while(i<100){
            i++;
            if (i%10 == 0){
                System.out.println();
                continue;
            }
            System.out.println(i);
        }
    }
}

8.练习

【练习1:计算0到100之间的奇数和偶数的和】

【练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个】

【练习3:打印九九乘法表】

public class Demo12practice {
    public static void main(String[] args) {
        int oddSum = 0;
        int evenSum =0;
        for (int i = 0; i < 100; i++) {
            if(i%2 !=0){
                oddSum +=i;
            }else{
                evenSum +=i;
            }
        }
        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);

    }
}
public class Demo12practice {
    public static void main(String[] args) {
        int oddSum = 0;
        int evenSum =0;
        for (int i = 0; i < 100; i++) {
            if(i%2 !=0){
                oddSum +=i;
            }else{
                evenSum +=i;
            }
        }
        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);

    }
}
public class Demo14practice {
    public static void main(String[] args) {
        for (int i = 0; i < 9; i++) {
            for(int j = 1; j <=i ;j++){
                System.out.print(j + "*"+ i+ "=" + (i*j)+"\t");
            }
            System.out.println();
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

疯丰

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

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

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

打赏作者

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

抵扣说明:

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

余额充值