A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 哒哒哒哒 初级黑马   /  2018-1-9 17:35  /  897 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*          for(初始化语句;判断语句;控制条件){
                                  循环体语句;
       }
      
       *(小括号里面的三条语句是帮我们控制循环执行多少次)
       *
       */
          
//          打印10个hello world
          for(int a = 1;a <= 10;a++){
                  System.out.println("hello world");
          }
          
//          求1到100的和
        /*  int sum = 0;
         for(int i = 0;i<=100;i++){
                 sum = sum + i;*/
//                 如果没有控制条件语句  无限循环
//                写在循环大括号里面的代码,每次都要循环
         
//         写在循环大括号外面的代码,只会在循环结束后才执行
        /* System.out.println(sum);*/
         
//                 记录在1-100以内有多少个偶数
        /* int count = 0;//用来记录偶数个数
         for(int ii =1;ii<=100;ii++){//计数器
                 if(ii%2 == 0){
                         count++;
                 }
         }System.out.println(count);*/
//         无限循环
//         for(;;){
//         System.out.println("无限循环");}
//         
         //记录1-100有多少个奇数
         int ji =0;
         for(int c =1;c<=100;c++){
                 
                 ji=(c%2!=0)?ji++:0;
                 }
         System.out.println(ji);
         
//         while(条件){} 循环
//         先要求输出10个"hello world"
         int b = 0;
         while (b<=10){
                 b++;
                 System.out.println("hello world");
         }
//         do{}....while();
         int j =0 ;
         do{
                 System.out.println("hello ");
                 j++;
         }while(j<=10);
         
         
//         do...while 和while  for循环的区别
//         如果开始条件不成立,while  for 都不会执行
//         但是do...while 至少执行一次

//        Random 作用:产生一个随机数
//                使用步骤 1.导包import java.util.Random;
//                      2.创建对象
//                      3.得到随机数
       
                /*Random b =new Random();// b 就是引用型数据类型  第二步
               
                int a =b.nextInt(100);//获取一个[0 100)在0-100范围内
                System.out.println(a);
                */
               
//                Scanner
//                使用步骤  1.导包import java.util.Scanner;
//                  2.创建对象
//                  3.得到输入数据
                /*Scanner scanner = new Scanner(System.in);
                int c = scanner.nextInt();
                System.out.println(c);*/

               
               
               
               
               
               
                //                猜数字小游戏案例
                Random a =new Random();
                int b = a.nextInt(100);
                Scanner c = new Scanner(System.in);
               
                int count = 0;
                System.out.println("请开始");
                while(true){
                       
                        int d = c.nextInt();
               
                        count++;
                        if(d>b){
                                System.out.println("大了");
                               
                        }else if(d<b){
                                System.out.println("小了");
                               
                        }else {
                               
                                System.out.println("yes");
                                break;
                        }
                       
                       
                }
                System.out.println("你猜了"+count+"回合");
               
               

5 个回复

倒序浏览
期待继续更新
回复 使用道具 举报
期待继续更新~~~
回复 使用道具 举报

期待继续更新~~~
回复 使用道具 举报
期待你每天的进步   温故而知新
回复 使用道具 举报
继续加油哦    
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马