Java学习(3)—— 流程控制

从键盘获取不同类型的值(用Scanner类):(1)导包:import java.util.Scanner;(2)创建Scanner的对象;(3)调用Scanner类的相关方法来获取指定类型的变量。

import java.util.Scanner; //导包
public class Test{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);  //实例化输入:创建对象

        System.out.println("输入名字:");
        String name = scan.next();   //获取字符串类型:next()

        System.out.println("输入年龄:");
        int age = scan.nextInt();  //获取整型:nextInt()

        System.out.println("输入体重:");
        double weight = scan.nextDouble(); //获取浮点型:nextDouble()

        System.out.println("是否健康:"); //true或者false
        boolean health = scan.nextBoolean(); //获取布尔型:nextBoolean()
    }
}

一.   if - else

import java.util.Scanner; //导包
public class Test{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);  //实例化输入:创建对象

        System.out.println("输入年龄:");
        int age = scan.nextInt();  //获取整型:nextInt()

        if(age < 18){
            System.out.println("未成年");
        }else if(age < 35){
            System.out.println("青年");
        }else if(age < 60){
            System.out.println("中年");
        }else{
            System.out.println("老年");
        }
    }
}

二.   switch - case

    switch后的表达式,只能是以下六种数据类型之一:byte、short、char、int、枚举类型、String

    case后只能是常量,不能是范围。  

    根据switch后表达式的值,依次匹配case中的常量。一旦匹配成功,则进入相应的case结构中,执行其语句。当执行完该语句后,则继续执行下面case结构中的语句,直到遇到break或者switch-case结构末尾为止。

import java.util.Scanner; //导包
public class Test{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);  //实例化输入:创建对象

        System.out.println("输入日期:");
        int number = scan.nextInt();  //获取整型:nextInt()

        switch(number){
            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

//遍历100以内的偶数,输出所有偶数的和,输出所有偶数的个数
public class Test{
    public static void main(String[] args) {
        int sum = 0;
        int count = 0;
        for(int i = 1; i <= 100; i++) {
            if (i % 2 == 0) {
                System.out.println(i);
                sum += i;
                count++;
            }
        }
        System.out.println("所有偶数的和:"+sum);
        System.out.println("所有偶数的个数:"+count);

        //System.out.println(i); 编译错误,i只在for循环内有效
    }
}

四.   while

//遍历100以内的偶数,输出所有偶数的和,输出所有偶数的个数
public class Test{
    public static void main(String[] args) {
        int sum = 0;
        int count = 0;
        int i = 1;
        while (i <= 100){
            if(i % 2 == 0){
                System.out.println(i);
                sum += i;
                count++;
            }
            i++;
        }
        System.out.println("所有偶数的和:"+sum);
        System.out.println("所有偶数的个数:"+count);
    }
}

五.   do - while

//遍历100以内的偶数,输出所有偶数的和,输出所有偶数的个数
public class Test{
    public static void main(String[] args) {
        int sum = 0;
        int count = 0;
        int i = 1;
        do{
            if(i % 2 ==0){
                System.out.println(i);
                sum += i;
                count++;
            }
            i++;
        }while(i <= 100);
        System.out.println("所有偶数的和:"+sum);
        System.out.println("所有偶数的个数:"+count);
    }
}

六.   while(true)和for(;;)

import java.util.Scanner;

//从键盘输入任意数量的数,判断其中正数和负数的个数
public class Test{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int count1 = 0;
        int count2 = 0;
        while(true){ //for(;;){
            int number = scan.nextInt();
            if (number > 0){
                count1++;
            }else if(number <0){
                count2++;
            }else if(number == 0){
                break;
            }
        }
        System.out.println("正数的个数:" + count1);
        System.out.println("负数的个数:" + count2);
    }
}

七.   嵌套循环

        求质数

//求100以内的质数
public class Test{
    public static void main(String[] args) {
        for(int i = 2; i <= 100; i++){
            boolean num = true;
            for(int j = 2; j < i; j++){
                if(i % j == 0){
                    num = false;
                    break;
                }
            }
            if(num == true){
                System.out.println(i);
            }
        }
    }
}

八.   break和continue

        break(默认结束当前包含此关键字最近的整个循环体)

        continue(结束本次循环,继续执行下一次循环)

public class Test{
    public static void main(String[] args) {
        for(int i = 1; i <= 3; i++){
            for(int j = 1; j<= 10; j++){
                if(j % 4 == 0){
                    break;
                }
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

         输出:123

                    123

                    123

public class Test{
    public static void main(String[] args) {
        for(int i = 1; i <= 3; i++){
            for(int j = 1; j<= 10; j++){
                if(j % 4 == 0){
                    continue;
                }
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

         输出:123567910

                    123567910

                    123567910

    结束指定标识的循环结构:

public class Test{
    public static void main(String[] args) {
        line:for(int i = 1; i <= 3; i++){
            for(int j = 1; j<= 10; j++){
                if(j % 4 == 0){
                    break line;
                }
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

         输出:123

public class Test{
    public static void main(String[] args) {
        line:for(int i = 1; i <= 3; i++){
            for(int j = 1; j<= 10; j++){
                if(j % 4 == 0){
                    continue line;
                }
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

         输出:123123123

         求质数(2):

//求100以内的质数
public class Test{
    public static void main(String[] args) {
        line:for(int i = 2; i <= 100; i++){
            for(int j = 2; j < i; j++){
                if(i % j == 0){
                    continue line;
                }
            }
            System.out.println(i);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值