鱼C论坛

 找回密码
 立即注册
查看: 3772|回复: 4

[学习笔记] JAVA学习Day4【循环结构】

[复制链接]
发表于 2020-8-16 16:31:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
学习视频:https://www.bilibili.com/video/BV17J411G72L?p=20

59-76
//循环结构
/*
*1、for(for(;;)死循环)
*2、while
*3、do...while
*/

class test04_01{
        public static void main(String[] args){
                //从键盘输入整数,输入0结束,统计输入的正数、负数个数
                java.util.Scanner input = new java.util.Scanner(System.in);
                int num;//声明在外面是为了在do{}外面仍然可以使用num
                int positive = 0, negative = 0;
                do{
                        System.out.print("请输入整数(0表示结束):");
                        num = input.nextInt();
                        if(num > 0)positive++;
                        else if(num < 0)negative++;

                }while(num != 0);
                System.out.println("正数的个数:" + positive);
                System.out.println("负数的个数:" + negative);

        }
}
/*
*在JAVA中,除了Math类中提供random()产生随机数
*还在java.util.Random类中提供产生随机数的方法
*/
class test04_02{
        public static void main(String[] args){
                java.util.Random tools = new java.util.Random();
                //获取随机整数
                int num = tools.nextInt();
                System.out.println("随机数是:" + num);
                //获取随机小数
                double d = tools.nextDouble();
                System.out.println("随机小数:" + d);
                //获取任意范围内的随机值
                int rangeNum = tools.nextInt(10);//[0,10)
                System.out.println("随机整数:" + rangeNum);

        }
}
/*
*break(1)用在switch...case中用来结束switch(2)用在循环中,用以结束循环
*/
class test04_03{
        public static void mian(String[] args){
                //从键盘输入整数,输入0结束,统计输入的正数、负数个数
                java.util.Scanner input = new java.util.Scanner(System.in);
                int temp1 = 0, temp2 = 0;
                while(true){
                        System.out.print("请输入整数:");
                        int num = input.nextInt();
                        if(num > 0)temp1++;
                        else if(num < 0)temp2++;
                        else{
                                break;
                        }
                }
                System.out.println("正数的个数:" + temp1);
                System.out.println("负数的个数:" + temp2);
                
        }
}

/*
*continue只能用于循环体,结束本次循环,执行下一次循环
*/
class test04_04{
        public static void main(String[] args){
                for(int i = 1; i <= 100; i++){
                        if(i % 2 != 0){
                                continue;
                        }
                        System.out.println(i);
                }
        }
}

/*
*break(1)如果在内循环中,结束的是内循环
*(2)如果在内循环外,外循环内,结束的是外循环
*/
class test04_05{
        public static void main(String[] args){
                for(int i = 1; i < 5; i++){
                        for(int j = 1; j < 5; j++){
                                if(i == j)break;
                                System.out.println("*");

                        }
                }
        }
}

//标签!!!!!!!!!!!!!!!!!!!!!!!!
class test04_06{
        public static void main(String[] args){
                //out是标签名称,和变量名一样
                out:for(int i = 1; i < 5; i++){
                        for(int j = 1; j < 5; j++){
                                if(i == j)break out;//跳出整个循环
                                System.out.println("*");

                        }
                }
        }
}

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-8-16 16:32:24 | 显示全部楼层
标签是个新概念,注意一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-16 16:33:58 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-16 16:46:03 | 显示全部楼层
糖逗 发表于 2020-8-16 16:32
标签是个新概念,注意一下

C 和 C++ 里都有。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-16 18:10:31 | 显示全部楼层

以前学的时候没太注意这个知识点后来不常用都忘记了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 08:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表