Java Control flow

Source: Internet
Author: User
Tags rand

Directory

    • 1. Reference data type
    • 2. Process Control Statements
      • 2.1-piece Control statement if
      • 2.2 If statement and ternary operator interchange
      • 2.3 Loop Statements
      • 2.4 Loop nesting
      • 2.5 Jump Statements
      • 2.6 Select structure Switch
    • 3, guess the number of cases
1. Reference data type

Scanner class
The scanner class belongs to the reference data type;
Format:数据类型 变量名 = new 数据类型();

/*    引用数据类型,介绍一个类 Scanner,这是应存在的类    定义引用数据类型变量,和基本类型变量有区别    格式:        数据类型 变量名 = new 数据类型();    举例:        Scanner sc = new Scanner();        创建出Scanner类的变量;    每个引用类型都有自己的功能,使用功能方式:        变量.功能名字()    Scanner类,作用是在命令行中接收输入    使用Scanner类步骤:        1.导入包,指明类所在的文件夹,关键字import;        2.公式,创建出Scanner类型变量;        3.变量,使用Scanner类中的功能,完成键盘输入;*/
import java.util.Scanner;public class Scanner_Reference{    public static void main(String[] args){        // 创建出Scanner类变量        Scanner sc = new Scanner(System.in);        // 功能1:nextInt()接收键盘输入,保证输入的是整数;        // 功能接收的数据就是整数,功能运行后的结果就是整数类型        int i = sc.nextInt();        System.out.println(i);        // 功能2:next()接收键盘输入的字符串        String s = sc.next();        System.out.println(s);    }}

Random class

/*    java中的引用类型  Random类作用是产生随机数    步骤:        1.导入包,Random类,也在java.util文件夹中        2.公式:创建出Random类型的变量        3.变量:调用Random类中的功能,产生随机数;    Random类中,nextInt()产生一个随机数,结果是int类型;        nextInt(写一个整数),产生的随机数在0 —— 指定的整数之间的随机数;        例如,nextInt(100),则范围在0-99    伪随机数:这种随机数是根据写好的算法生成的一个伪随机数;*/
import java.util.Random;public class Random_Reference{    public static void main(String[] args){        // 创建一个Random类型的变量        Random rand = new Random();        // 功能1:调用Random类中的功能,产生一个随机整数;        int i = rand.nextInt(100);        System.out.println(i);        // 功能2:调用Random类的功能,产生一个随机浮点数;        double d = rand.nextDouble();        System.out.println(d);    }}
2. Process Control Statement 2.1 Control statement if

If statement
if (conditional statement) {EXECUTE statement; ...}

/*    if语句:        编码格式            格式:        if(条件语句){            if语句的执行体,即执行语句;        }            条件语句:必须是布尔类型 1==1  true||false            执行体:当if中的条件为true时,执行这个执行体;                    当if中的条件为false时,什么也不做;*/
public class if_control{    public static void main(String[] args){        int i = 5;        if(i >= 5){            System.out.println("我是"+i+"号");            i++;        }        System.out.println("我号码增大: "+i);       // System.out.println(i);    }}

If...else statements
Format: if (conditional statement) {EXECUTE statement 1;} else{EXECUTE statement 2;}

/*    if...else 语句格式        编写格式            if(条件){                if的执行体            }            else{                else的执行体            }        当if中的条件为true时,执行if的执行体;        当if中的条件为false时,执行else的执行体;*/
public class ifelse_control{    public static void main(String[] args){        int i = 25;                if(i%2 == 0){            System.out.println(i+"是偶数");        }        else{            System.out.println(i+"是奇数");        }    }}

If ... (else IF) ... else statement

/*    if...elseif...if 语句    适合在程序中,实现多条件的判断,条件的个数没有限制;    编写格式:        if(条件){            执行体1;        }        else if(条件){            执行体2;        }        else{            执行体3;        }*/
public class if_elseif_else{    public static void main(String[] args){        //典型案例,成绩分类        int i = 88;                if(i>=90){            System.out.println(i+"是优秀");        }        else if(i>+80){            System.out.println(i+"是良好");        }        else if(i>=70){            System.out.println(i+"是中等");        }        else if(i>=60){            System.out.println(i+"是及格");        }        else{            System.out.println(i+"不及格");        }    }}
2.2 If statement and ternary operator interchange
/*    if语句和三元运算符的替换    要求:计算两个数的最大值    例如:两个整数比较大小        使用if和三元运算符的情况:        (1)判断条件多,则使用if;        (2)三元,必须是有结果的,if可以没有结果;*/
public class if_Ternary_replace{    public static void main(String[] args){        int i = 5;        int j = 5;                // 使用if语句,判断出最大值        if(i>j){            System.out.println(i+"是最大值");        }        else{            System.out.println(j+"是最大值");        }                // 使用三元运算实现        int k = i>j?i:j;        System.out.println(k+"是最大值");    }}
2.3 Loop Statements While Loop
/*    While循环:        编码格式:            while(条件){                循环体            }                        条件:当条件是true,就执行循环体,执行完循环体后,程序再次判断条件是否为true,如果是则继续执行循环体,否则循环结束;            循环体:循环体和if执行体一样,都是语句;*/
public class while_circle{    public static void main(String[] args){                int i = 1;        while(i<7){            System.out.println("互联网思维"+i);            i++;        }    }}
For Loop
/*    for循环        编码格式            for(初始化变量; 条件; 增量){                循环体            }                        初始化变量:定义变量,作用,控制循环的次数;            条件:当条件是true,执行循环体,条件是false,结束循环            增量:变量自增情况,例如:i++,i=i+2*/
public class for_circle{    public static void main(String[] agrs){        // 定义变量,用于求阶乘        double factorial = 1;        int i,j=0;                // for循环        for(i=10; i>0; i--){            System.out.println(i);            factorial = factorial*i;            j++;        }        System.out.println(‘/n‘+j+"的阶乘是"+factorial);    }}
Do...while Cycle
/*    do...while循环        编写格式:            do{                循环体;            }while(条件);                    特点:无条件先执行一次;*/
public class do_while_circle{    public static void main(String[] args){        int i = 1;        do{            System.out.println("x="+i);            i++;        }while(i<10);    }}
dead Loop
while死循环;while(true){}for死循环;for(;true;){}
2.4 Loop nesting
/*    循环嵌套:    for(){        for(){            循环体        }        (循环体)——可以不存在    }    总循环次数=外循环次数*内循环次数;    内循环是外循环的循环体;外循环控制行数,内循环控制每行个数;*/

To print a partial right triangle:

public class for_Nesting{    public static void main(String[] args){        // 打印由星星组成的三角形        for(int i=0; i<9; i++){            for(int j=0; j<5; j++){                                if(i<=4){                    if(j<=i)                        System.out.print("* ");                }                else{                    if(j>=i-4)                        System.out.print("* ");                }            }            System.out.println();        }    }}

The output is:

** ** * ** * * ** * * * ** * * ** * ** **

Print out Diamond

public class for_Nesting{    public static void main(String[] args){        // 打印由星星组成的菱形        for(int i=0; i<9; i++){            if(i<5){                for(int k=4-i; k>0; k--)                    System.out.print(" ");            }            else{                for(int k=i-4; k>0; k--)                    System.out.print(" ");            }            for(int j=0; j<5; j++){                                if(i<=4){                    if(j<=i)                        System.out.print("* ");                }                else{                    if(j>=i-4)                        System.out.print("* ");                }            }            System.out.println();        }    }}

The output is:

    *   * *  * * * * * * ** * * * * * * * *  * * *   * *    *
2.5 Jump Statements Break Statement
/*    break 关键字    作用于循环中,终止循环的作用;    但是,break终止的是所在位置的循环,但是不能终止以外的循环;    switch语句中的表达式的数据类型,是有要求的    JDK1.0 - 1.4 数据类型接收 byte short int char    JDK1.5       数据类型接收 byte short int char enum(枚举)    JDK1.7       数据类型接收 byte short int char enum(枚举) string*/
public class break_jump{    public static void main(String[] args){        for(int i=0; i<10; i++){            for(int j=0; j<10; j++){                System.out.print("j="+j);                break;            }            System.out.println("i="+i);        }          }}

There is a break that terminates the specified loop position:

public class break_jump{    public static void main(String[] args){        a:for(int i=0; i<10; i++){            for(int j=0; j<10; j++){                System.out.print("j="+j);                break a;            }            System.out.println("i="+i);        }          }}
Continue statements
/*    continue关键字    作用是终止本次循环,进入到下一个循环;    */
public class continue_jump{    public static void main(String[] args){        for(int i=1; i<10; i++){            if(i%5!=0){                continue;            }            else{                System.out.println("5的倍数有:"+i);            }        }            }}
2.6 Select structure Switch
/*    选择结构   switch语句    编写格式        switch(表达式){            case 常量1:                要执行的语句;            break 常量2;            case 常量2:                要执行的语句;            break;            case 常量3:                要执行的语句;            break;            default:                要执行的语句;            break;        }        执行流程:表达式和case后面的常量进行比较            匹配到哪个case常量,就执行对应语句;遇到break后,就结束;*/
public class switch_opt{    public static void main(String[] args){        // 定义变量,保存星期位        int week = 6;        // switch语句        switch(week){            case 1:                System.out.println("星期"+week);            break;            case 2:                System.out.println("星期"+week);            break;            case 3:                System.out.println("星期"+week);            break;            case 4:                System.out.println("星期"+week);            break;            case 5:                System.out.println("星期"+week);            break;            case 6:                System.out.println("星期"+week);            break;            default:                System.out.println("星期"+week);            break;        }    }}
Case Statement

Switch characteristics: case penetration, (regardless of the previous case, directly backward execution, that is, do not match case, go down)
If the execution statements of multiple case statements are the same, then the execution statement only needs to be written once.
Case 1 Case 2 ... case 5 outputs the same result, it can be written as:

switch(week){        case 1:        case 2:        case 3:        case 4:        case 5:                System.out.println("工作日");        break;        case 6:        case 7:                System.out.println("休息日");        break;}
3, guess the number of cases
/*    在一定范围内猜数字    (1)随机数生成1-100的固定数,用Random;            键盘输入,用Scanner;    (2)作比较,和固定数做比较,用if结构;    (3)不断猜数字,直到猜正确,用while结构;*/
import java.util.Random;import java.util.Scanner;public class guess_number{    public static void main(String[] args){        System.out.println("猜数字游戏开始了");        System.out.println("输入1-100的数字:");        // 创建Random类        Random rand = new Random();        int randNumber = rand.nextInt(100)+1;  //使用功能nextInt()获取数字输入;                // 创建Scanner类变量        Scanner sc = new Scanner(System.in);                while(true){            int number = sc.nextInt();            if(number>randNumber){                System.out.println("猜大了");            }            else if(number<randNumber){                System.out.println("猜小了");            }            else{                System.out.println("恭喜你,猜中了");                break;            }        }    }}

Java Control flow

Related Article

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.