12月26日学习内容

While循环

例子:

import java.util.Arrays;
public class Lx{public static void main(String[] args) {
    int x = 10;
    while(x < 20){
        System.out.print("volue of x : " +x);
        x++;
        System.out.print("\n");
    }
}} ```
##Do   While循环
例子:

import java.util.Arrays;
public class Lx{public static void main(String[] args) {
int x = 10;
do{
System.out.print("value of x : "+x);
x++;
System.out.print("\n");
}while(x<20);
}}```

条件判断

在Java中条件判断有两种形式:if和 switch

if 语句由一个布尔表达式后跟一个或多个语句组成。

if 语句的语法是:

if(Boolean_expression){ //Statements will execute if the Boolean expression is true}```
如果布尔表达式的值为 true,那么代码里面的块 if 语句将被执行。如果不是 true,在 if 语句大括号后结束后的第一套代码将被执行。
####例子

public class Lx{ public static void main(String args[]){
int x = 10; if(x < 20 )
{ System.out.print("This is if statement");
} } }```

if...else 语句

任何 if 语句后面可以跟一个可选的 else 语句,当布尔表达式为 false,语句被执行。

if...else 的语法是:

if(Boolean_expression){
//Executes when the Boolean expression is true}else{
//Executes when the Boolean expression is false}```
####例子

public class Lx{ public static void main(String args[]){
int x = 30;
if( x < 20){
System.out.print("This is if statement");
}else{System.out.print("This is else statement");}
} } ```

当使用 if , else if , else 语句时有几点要牢记。

一个 if 语句可以有0个或一个 else 语句 且它必须在 else if 语句的之后。
一个 if 语句 可以有0个或多个 else if 语句且它们必须在 else 语句之前。
一旦 else if 语句成功, 余下 else if 语句或 else 语句都不会被测试执行。

例子

public class Lx{ public static void main(String args[]){ 
    int x = 30;
    if ( x == 10){
        System.out.print("Value of x is 10");
    }else if ( x == 20){
        System.out.print("Value of x is 20");
    }else if ( x == 30){
        System.out.print("Value of x is 30");
    }else{
        System.out.print("This is else statement");
    }
} } ```
####嵌套 if...else 语句与逻辑与(&&)
它始终是合法的嵌套 if-else 语句,这意味着你可以在另一个 if 或 else if 语句中使用一个 if 或 else if 语句。
#####例子

public class Lx{ public static void main(String args[]){
int x = 30;
int y = 10;
if ( x == 30){
if( y == 10){
System.out.print("x = 30 and y = 10");
}
}
} } ```

作业练习,建立一个数组,把元素从小到大排列

import java.util.Arrays;
public class Lx{public static void main(String[] args) {
    int [] array = {2,8,1,9,5,4};
    int temp;  for (int i = 0; i < array.length; i++)
    {   for (int j = 0; j < array.length;j++)
    {   if (array[j] > array[i])
    {   temp = array[i];
        array[i] = array[j];
        array[j] = temp; 
    }
    }
    }
    for (int i = 0; i < array.length; i++)
    {System.out.print(array[i]+" ");}
}} ```

你可能感兴趣的:(12月26日学习内容)