栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 <= 4; 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);
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/298969.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号