03_java Basic Grammar _ 3rd day (Scanner, Random, Flow control statement) _ Handout

Source: Internet
Author: User

Introduction of today's content
1. Creation and use of reference type variables
2. Selection statement of Process Control statement
3. Loop statement of Flow control statement
4, Cycle advanced

01 Creating a reference type variable formula
* A: 创建引用类型变量公式    * a: 我们要学的Scanner类是属于引用数据类型,我们先了解下引用数据类型。    * b: 引用数据类型的定义格式        * 与定义基本数据类型变量不同,引用数据类型的变量定义及赋值有一个相对固定的步骤或格式。        * 数据类型  变量名  =  new 数据类型();    * c: 引用数据类型的使用        * 每种引用数据类型都有其功能,我们可以调用该类型实例的功能。        * 变量名.方法名();    
Use of the 02Scanner class
* A: Scanner类的使用    * a: 导包import java.util.Scanner;    * b:创建键盘录入对象 Scanner sc = new Scanner(System.in);    * c: 读取键盘录入的一个整数        * int enterNumber = sc.nextInt();    * d: 读取键盘录入的字符串        * String enterString = sc.next();* B: 案例代码    import java.util.Scanner;    public class Demo05Scanner{        public static void main(String[] args)         {            Scanner sc = new Scanner(System.in);            int enterNumber = sc.nextInt();            System.out.println("用户输入的整数为"+enterNumber);            String enterString = sc.next();            System.out.println("用户输入的字符串为"+enterString);        }    }                
Use of the 03Random random number class _1
* A: Random随机数类的使用_1    * a: 功能        * 生成随机数需要使用到引用类型随机数Random类    * b: 使用方式        * import导包:所属包java.util. Random        * 创建实例格式:Random  random = new Random ();        * 调用方法            * nextInt(int maxValue) 产生[0,maxValue)范围的随机数,包含0不包含maxValue            * nextDouble()  产生[0,1)范围的随机数            如:                Random  random = new Random ();                int  myNumber = random.nextInt(100);//结果为0-99的一个数* B: 案例代码    import java.util.Random;    public class RandomDemo{        public static void main(String[] args){           Random ran = new Random();           // Random类中的,产生随机数的功能           int i = ran.nextInt(100);           System.out.println(i);                      //问题? 产生随机数,范围 1-100之间           // nextInt(100) 0-99 + 1        }    }
Use of the 04Random random number class _2
* A: Random随机数类的使用_2    * a: 调用方法        * nextDouble()  产生[0,1)范围的随机数        如:            Random  random = new Random ();            int  myNumber = random.nextDouble();//结果为0.0-1.0之间的数(包括0.0不包括1.0)
The first of the 05IF statement formats
* A: if语句格式第一种    * a: 书写格式        if(比较表达式) {            语句体;        }    * b:执行流程:        * 先计算比较表达式的值,看其返回值是true还是false。        * 如果是true,就执行语句体;        * 如果是false,就不执行语句体;* B: 案例代码    public class IfDemo{        public static void main(String[] args){              int i = 5 ;              //对变量i进行if判断              if(i > 5){                  System.out.println("if中的条件是true");                  i++;              }                            System.out.println(i);        }    }                
06IF statement format the second type
* A: if语句格式第二种    * a: 书写格式        if(比较表达式) {            语句体1;        }else {            语句体2;        }    * b:执行流程:        * 首先计算比较表达式的值,看其返回值是true还是false。        * 如果是true,就执行语句体1;        * 如果是false,就执行语句体2;* B: 案例代码    public class IfElseDemo{        public static void main(String[] args){             int i = 16 ;             //判断变量,是奇偶数, 除以2,看余数是0还是1             if( i % 2 == 0 ){                 System.out.println(i+" 是偶数");             }else{                 System.out.println(i+" 是奇数");             }        }    }
The third type of 07IF statement format
* A:IF Statement Format third * A: Write format if (comparison expression 1) {statement Body 1;            }else if (comparison expression 2) {statement body 2;            }else if (comparison expression 3) {statement Body 3;            } ... else {statement body n+1;        } * B: Execution Process: * First evaluates the comparison expression 1 to see if its return value is TRUE or FALSE, * If true, executes the statement body 1,IF statement end.        * If False, then evaluates the comparison expression 2 to see if its return value is TRUE or FALSE, * If true, executes the statement body 2,IF statement end. * If False, then the comparison expression 3 is evaluated to see if the return value is TRUE or FALSE, * if all is false, the statement body n+1 is executed. * B: Case code public class ifelseifdemo{public static void Main (string[] args) {//score judgment required, score >80 score &G            t;70 result >60//define variable, save score int grade = 75;            Use if ELSE if statement to judge the result if (Grade >) {System.out.println (grade+ "score is excellent");            }else if (Grade >) {System.out.println (grade+ "score is good");    }else if (Grade >) {System.out.println (grade+ "score is Medium");        }else{System.out.println (grade+ "score is poor"); }                    }    }
Interchange of 08IF statements and ternary operators
* A: 三元运算符    * a: 概念        * 用来完成简单的选择逻辑,即根据条件判断,从两个选择中选择一种执行    * b: 使用格式        * (条件表达式)?表达式1:表达式2;    * c: 运算规则        * 1: 判断条件表达式,结果为一个布尔值        * 2: true,运算结果为表达式1        * 3: false,运算结果为表达式2* B: 案例代码    public class IfElseDemo_1{        public static void main(String[] args){            int j = 6;            int i = 15;            //使用if语句,判断出最大值            if(i>j){            int j = 6;                System.out.println(i+" 是最大值");            }else{                System.out.println(j+" 是最大值");            }                        //使用三元运算实现            int k = i>j ? i : j;            System.out.println(k+" 是最大值");        }    }* C: 使用if语句还是三元表达式    * 判断条件多,使用if    * 三元,必须有结果的, if 可以没有结果的
09while Cycle
* A: while循环结构    * a: 使用格式        初始化表达式;        while(条件){            循环体        }    * b: 执行顺序          当条件是true,就执行循环体,执行完循环体后          程序再次执行while中的条件,如果条件还是true,继续执行循环体          直到条件是false的时候,循环就结束* B: 案例代码    public class WhileDemo{        public static void main(String[] args){            //输出 1-4之间的整数            //定义变量,整数类型, 循环的条件            int i = 1;            while( i < 5 ){                System.out.println(i);                i++;            }        }    }
10for Cycle _1
* A: for循环_1    * a: 使用格式         for(初始化变量 ; 条件 ; 增量){             循环体;         }    * b: 各模块解释        初始化变量: 定义变量,作用是用来控制循环的次数        条件: 当条件是true,执行循环体,条件是false,结束循环        增量: 变量自增情况 * B: 案例代码    public class ForDemo{        public static void main(String[] args){            //for循环,输出0-10            for(int i = 0 ; i < 11 ; i++){                System.out.println(i);            }        }    }    
11for Cycle _2
* A: for循环的执行流程    for(① ; ② ; ③){        ④    }    第一步,执行①    第二步,执行②,如果判断结果为true,执行第三步,如果判断结果为false,执行第五步    第三步,执行④    第四步,执行③,然后重复执行第二步    第五步,退出循环    
12for Cycle _3
* A: 案例    * a: 利用for循环,计算1+4的结果* B: 案例代码    public class ForDemo_1{        public static void main(String[] args){            // 定义变量,记录求和后的数据            int sum = 0;            // 利用循环,将变量从1变化到4            for(int i = 1 ; i <= 4 ; i++){                //对变量进行求和                sum = sum + i;            }            System.out.println(sum);        }    }
13do_while Cycle
* A: do_while循环    * a: 使用格式        do{           循环体;        }while(条件);    * b: 执行顺序        先执行一次循环体,然后再判断条件,如果条件为true,继续执行循环体,        如果条件为false,循环结束。    * c: 特点        * 无条件先执行一次* B: 案例代码    public class DoWhileDemo{        public static void main(String[] args){            int i = 0;             do{                System.out.println(i);                i++;            }while( i <  5);        }    }
14 dead Loop
* A: 死循环概述    * 无限循环存在的原因是并不知道循环多少次,而是根据某些条件,来控制循环* B: 死循环格式    * while(true){}    * for(;;){}         
15 Nesting for Loop _1
* A: 嵌套循环的概述    * 嵌套循环是指在一个循环语句的循环体中再定义一个循环语句的语法结构。    * while、do…while、for循环语句都可以进行嵌套,并且它们之间也可以互相嵌套,    * 如最常见的在for循环中嵌套for循环。* B: 嵌套循环的格式    for(初始化表达式; 循环条件; 操作表达式) {        ………        for(初始化表达式; 循环条件; 操作表达式) {            执行语句            ………        }        ………    }* C: 各模块解释    * 总的循环次数 =  内循环次数 * 外循环的次数    * 内循环,是外循环的循环体           * 外循环,控制的是行数    * 内循环,控制的是每行的个数
16 Nesting for Loop _2
* A: 案例    * a: 打印正三角形* B: 案例代码    public class ForForDemo{        public static void main(String[] args){            for(int i = 0 ; i < 9 ; i++){                for(int j = 0; j < i+1 ;j++){                    System.out.print("* ");                }                System.out.println();            }        }    }
17break statements
* A: break语句    * a: 作用        * 跳出所在的循环体    * b: 书写位置        * 必须出现在循环或选择结构内    * c: 举例        for(int i=0; i<10; i++) {            if(i>5) {            break;        }            System.out.println(“我爱Java”+i);        }        //会从0-5输出6次“我爱Java”* B: break详细解释    * a: 作用        * 在loop/switch选择或者循环过程中,我们总是满足布尔表达条件才能执        * 行对应的代码,然而在这些逻辑过程中,            可以使用一些关键字直接跳出正在执行的代码,去执行后边或者指定位置的代码,            这些关键字一旦出现就可以跳转语句执行顺序。    * b: 使用方式        * 无法单独使用,必须将break关键字置于switch或循环语句中    * c: 运行规律        * 不需要判断任何条件,只要遇到break变直接跳出执行后续代码。会完全跳        * 出选择或者循环结构        * 只能跳出最近的代码块,不能跨越多级代码块* C:循环标号    * a: 为什么使用循环标号        * 当在双层循环或者循环内有switch选择语句时,我们发现,使用break或者continue        * 所作用的对象均是内层语句,无法直接跳出外层循环,这时就需要使用标号语句跳转了.    * b: 使用方式        * 在外层循环外的某行前边,使用后边跟有冒号”:”的标识符,即定义完毕。          使用时当在内层循环使用break或continue时后边紧跟之前定义的标号即可    * c: 运行规律        * 当外层循环外定义了标号        * 内层使用break,终止内外双层循环。        * 内层使用continue,终止内层循环,继续外层循环。
18continue statements
* A: continue语句    * a: 作用        * 提前结束本次循环,继续进行下次循环    * b: 使用方式        * 无法单独使用,必须将continue关键字置于循环语句中    * c:运行规律        * 不需要判断任何条件,只要遇到continue变直接跳出本轮循环进行下次循环    * d:案例代码        public class ContinueDemo{            public static void main(String[] args){                for(int i = 0 ; i < 10 ; i++){                    if(i%2==0){                        continue;                    }                    System.out.println(i);                }            }        }        //会把0-9之间所有的奇数打印到控制台上
19 Guessing number games
* A: Guess the number of games * A: Analysis * The number of users may be greater than, less than, or equal to the number of guesses, so there will be three cases, * with the preceding ternary operator can be implemented, but with the ternary operator nesting, compared to hemp Bother! Can be judged in a simpler way if condition, can have more than three conditions * B: Requirements analysis * Background pre-generated a random number 1-100, the user keyboard input guessing number * If guessed right, print "Congratulations, correct" * if        Guess wrong * guess big: print "Sorry, you guessed big!"                * Guess small: print "Sorry, you guessed small!" Until the number guessed up to 5 times, otherwise prompted "Sorry, you have no chance!" * B: Case code//Guess the number of small games to complete guess number games: 1, generate a random number of background pre-generated a random number 1-100, the user keyboard input guessing the number 2, through the IF statement on the user        Compare the number of guesses to the random number if you guessed it, print "Congratulations, correct it" if you guessed the wrong guess big: print "Sorry, you guessed big!"        Guess small: print "Sorry, you guessed small!"    3, through the for loop to complete the user guessing cycle until the number can guess up to 5 times, otherwise prompted "Sorry, you have no chance!"    */import Java.util.Random;    Import Java.util.Scanner; You can import all the classes under the package one at a time by *, but it is not recommended.    Which one to use is recommended.    Import java.util.*; public class guessnumber{public static void Main (string[] args) {///1, Generate random number//background pre-generate a random number 1-1 00, user keyboard input Guess number//create random Number object randomly random = new RAndom ();            Generates a 1-100 random number int randomnumber = random.nextint (100) +1; System.out.println ("I'm generating random numbers:" +randomnumber+ "What do you guess?              ");            Cheat Dedicated//Generate console entry for Scanner object Scanner sc = new Scanner (system.in);                3, through the for loop to complete the user guessing cycle//through the For loop to complete guessing the number logic for (int i=1; i<=5; i++) {//Prompts the user to enter the number to guess, with the variable to receive                System.out.println ();                System.out.println ("Please enter a number of 1-100:");                                int guessnumber = Sc.nextint (); 2. Compare the number of guesses with the random number by the IF statement//If you guessed the right if (guessnumber==randomnumber) {///print guess right after Hint System.out.println ("Congratulations, guess right!")                    ");                Jump out of the loop, no longer guess break; }else {//If you guessed the wrong//If you guessed the big if (guessnumber>randomnumber) {Syst                    Em.out.println ("Sorry, you guessed big!");         }else {//If the Guess is small               System.out.println ("Sorry, you guessed small!"); }}//If you guessed the last 5th time and still don't guess right, jump out of the loop if (i==5) {System.out.println ("Sorry, too back, come again next time!")                    ");                Break }//Every time after guessing the wrong, are prompted how many chances System.out.println ("Please note, you also have" + (5-i) + "chance, please answer carefully!            "); }        }    }

03_java Basic Syntax _ 3rd day (Scanner, Random, Flow control statement) _ Handout

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.