Zero-based learning java ------ day4 ------ flow control structures

1. Sequence Structure

Code execution from the down turn

2. Select structure

Also known as the branch structure, which will be selected in accordance with different code execution result of the execution of the following two forms:

  The if statement

  switch statement

2.1 if statement

The first form 2.1.1 if statement

format:

if (relational expression) { 
        statement body; 
} 
execution process: 
            First, the relational expression is determined to see the result is true or false 
            If true statement executes the body 
            if the body is a false statement is not executed  

 Case

. 1  Package com._51doit.javase.day04;
 2  
. 3  Import java.util.Scanner;
 . 4  
. 5  public  class ifDemo1 {
 . 6      public  static  void main (String [] args) {
 . 7          Scanner SC = new new Scanner (the System.in);
 . 8          System.out.println ( "enter first integer:" );
 . 9          int a = sc.nextInt ();
 10          IF (a>. 3 ) {
 . 11              System.out.println ( "ha" );
 12 is              
13 is          }
 14         System.out.print ( "hey" );
 15      }
 16      
. 17 
18 is }

Results of the:

Note the if statement

   if statement braces can be omitted, once omitted, only the end of the first statement to the control ( only until the first semicolon )

   When we developed, do not write, then the problem is difficult to debug

 For example: The code as follows

package com._51doit.javase.day04;

public class IfDemoTest {
    public static void main(String[] args) {
        if(3==4)
            System.out.println("哈哈");
            System.out.println("呵呵");
    }
}

Run the code above results Oh

 Explained: if only the end of the first statement to a control (i.e., after the semicolon until the first if)

 If if (3 == 4) change if (3 == 4);  added a semicolon, operating results for the Oh ha

 Semicolon ";" corresponds {;} (which represents the empty statement, showing no sand honey beans)

 

Exercise:

Two integers from the keyboard input, it is determined whether or not the two data are equal,
if the outputs are: equal to,
not the output are not equal
1 if statement is completed
2. Use an if statement is completed

public  class IfTest {
     public  static  void main (String [] args) {
         // 1. From the keyboard input two integers 
        Scanner SC = new new Scanner (the System.in); 
        System.out.println ( "Enter first integer" );
         int A = sc.nextInt (); 
        System.out.println ( "Please enter the second integer" );
         int B = sc.nextInt ();
         // 2. if statement determines do 
        / * if (A B ==) { 
        System.out.println ( "equal"); 
        } 
        IF (A = B)! { 
        System.out.println ( "not equal"); 
        }* / 
        // use a statement to achieve 
        String re = "equal" ;
         IF (! A = B) { 
            Re = "not equal" ; 
        } 
        System.out.println (Re); 
        } 
}

 

The second form 2.1.2 if statement

format

if (relational expression) { 
              statement 1; 
} {the else 
              sentence 2; 
} 
execution flow 
is first determined result to see a relational expression is true or false
     If true statement 1 is executed
if the statement is false to perform 2

 Case:

/ ** 
 keyboard input an integer and judgment data is odd or even 
 * / 

Package com._51doit.javase.day04; 

Import java.util.Scanner; 

public  class IfDemo2 {
     public  static  void main (String [] args) { 
        Scanner SC = new new Scanner (the System.in); 
        System.out.println ( "enter a data" );
         int a = sc.nextInt ();
         IF (a% 2 == 0 ) { 
            System.out.println ( "even" ); 
        } the else { 
            System.out.println ( "odd" ); 
        }
    }
}

 

The third form 2.1.3 if statement

format

IF (relational expression 1) { 
                statement body 1; 
} the else  IF (relational expression 2) { 
                statement body 2; 
} 
        ... 
the else { 
                statement body n- + 1'd ; 
} 
procedure: 
First, the relational expression 1 is determined to see which is the true result or false 
if the statement is true on the implementation of 1 
if it is false to continue to determine the relationship between the expression 2 to see the result is true or false 
if it is true the statement is executed 2
if it is false to continue to determine the relationship between the expression ... see the result is true or false
if there is no relationship between the expression is true, then the statement is executed body n + 1.

Case

public class IfDemo4 {
    //从键盘录入一个整数,判断是正数,负数,还是0
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int a = sc.nextInt();
        if(a>0) {
            System.out.println("正数");
        }else if(a==0) {
            System.out.println("是0");
        }else {
            System.out.println("负数");
        }
    }
}        

 

 练习

1. 键盘录入x 的值,计算出y 的并输出。
x>=3         y = 2x + 1;
-1<=x<1    y = 2x;

 x<-1          y = 2x – 1;

 代码如下:

 1 package com._51doit.javase.day04;
 2 import java.util.Scanner;
 3 public class IfTest2 {
 4     public static void main(String[] args) {
 5         Scanner sc = new Scanner(System.in);
 6         System.out.println("请输入x的值");
 7         int x = sc.nextInt();
 8         if(x>=3) {
 9             System.out.println("y="+(2*x+1));
10         }else if(x<1&x>=-1) {
11             System.out.println("y="+2*x);            
12         }else if(x<-1) {
13             System.out.println("y="+(2*x-1));
14         }else {
15             System.out.println("x不合法");
16         }        
17     }
18 }

 

2.2  switch语句

 格式

switch(表达式){
case 常量值1:
    语句体1;
    break;
case 常量值2:  
    语句体2;
    break;
...
default:
    语句体n+1;
    break;
}

 执行流程:

1. 首先计算出表达式的值

2. 其次,和case做比较,一旦有对应的值,就会执行相应的语句,在执行的过程中,遇到break就会结束,否则一直往下进行

3. 最后,如果所有的case都和表达式的值不匹配,就会执行default语句体部分,然后程序结束掉。

表达式的类型

 byte,short,int, char, 枚举   jdk7以后(新加):String

案例:

根据键盘录入的数值1,2,3,.....7  输出对应的星期一,星期二,。。。星期日

package com._51doit.javase.day04;

import java.util.Scanner;

public class SwithDemo {
    public static void main(String[] args) {
        // 输入1-7中的一个数,分别代表周一到周日
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int day = sc.nextInt();
        switch(day) {
        case 1:
            System.out.println("Monday");
            break;
        case 2:
            System.out.println("Tuesday");
            break;
        case 3:
            System.out.println("Wednesday");
            break;
        case 4:
            System.out.println("Thursday");
            break;
        case 5:
            System.out.println("Friday");
            break;
        case 6:
            System.out.println("Saturday");
            break;
        case 7:
            System.out.println("Sunday");
            break;
        default:
            System.out.println("Are you kidding?");
        }    
    }
}

 

注意事项:

1. case后面只能跟常量,不能跟变量

2. 多个case后面的常量值不能相同

3. case的顺序没有要求,可以放到任意位置

4. default也可以放在任意位置

5. default可以不要

6.break可以省略,如果省略的话,代码会继续向下执行,不管下面的case是否匹配成功,一直执行到再次遇到break,或者是执行到了switch语句结束

如若将所有break不写上面代码运行结果会变成如下,当输入1时:1的结果连同其后面的结果都会打印出来,当输入2时,21的结果连同其后面的结果都会打印出来(1的不会)

7 switch语句何时结束:遇到break,或者代码执行到了switch语句的最后

 

3. 循环结构

让一段代码反复执行很多次

3.1 for 循环

for(初始化语句;判断条件语句;控制条件语句) {
    循环体语句体;
}

执行流程:

(1)执行初始化语句

(2)执行判断条件语句,看其结果是true还是false,如果是false,循环结束

(3)执行循环体语句

(4)执行控制条件语句

(5)回到(2)继续

案例:

1  打印100句话

package com._51doit.javase.day04;
// 打印100句话
public class ForDemo{
    public static void main(String[] args) {
        for(int i=1;i<100;i++) {
            System.out.println("学大数据的我");
        }
    }
}

2. 求1-100奇数和偶数和

 

package com._51doit.javase.day04;

public class ForDemo{
    public static void main(String[] args) {
        int oldNumberCount = 0;
        int evenNumberCount = 0;
        for(int i=1;i<=100;i++) {
            if(i%2==0) {
                evenNumberCount += i;
            }else {
                oldNumberCount += i;
            }
        }
        System.out.println("1-100中的偶数和为"+evenNumberCount);
        System.out.println("1-100中的奇数和为"+oldNumberCount);

3. 请统计1-1000之间分别满足如下条件的数据有多少

    对3整除余2;对5整除余3;对7整除余2;

/*
 请统计1-1000之间分别满足如下条件的数据有多少个

    对3整除余2;对5整除余3;对7整除余2;
 **/

package com._51doit.javase.day04.loop;

public class ForTest {
    public static void main(String[] args) {
        int a=0;
        int b=0;
        int c=0;
        for(int i=1; i<=1000; i++) {
            if(i%3 == 2) {
                a += 1;
            }
            if(i%5 ==3 ) {
                b += 1;
            }
            if(i%7 == 2) {
                c += 1;
            }    
        }
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
    }
}

注意,此处不能用else if(要用if),因为else if只要满足条件,后面的代码就不会运行了,但有些数据可能既满足条件1也满足条件2或3,这样就会漏算

 3.2 while循环

 格式:

 初始化语句;
 while(判断条件语句) {
     循环体语句体;
     控制条件语句;
 }

案例

1. 打印1-100的数

package com._51doit.javase.day04.loop;

public class WhileDemo {
    public static void main(String[] args) {
        int i = 1;
        while(i <= 100) {
            System.out.println(i);
            i++;
        }
    }
}

2. 一座山峰的高度为8848m,加入有一张足够大的纸,厚度为0.01m,请问需要折多少次才能保证纸的厚度不低于山峰的高度

/*
 一座山峰的高度为8848m,加入有一张足够大的纸,厚度为0.01m,请问需要折多少次才能保
 证纸的厚度不低于山峰的高度
 **/

package com._51doit.javase.day04.loop;

public class WhileDemo2 {
    public static void main(String[] args) {
        double hight = 0.01;
        int num = 0;
        while(hight < 8848) {
            hight *=2;
            num++;
        }
        System.out.println(num);
    }
}

 

两种循环的对比:

     for 循环适合针对一个范围判断进行操作

     while 循环适合不知道循环的次数,或者要求循环的次数

   

 

Guess you like

Origin www.cnblogs.com/jj1106/p/11294293.html