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]+" ");}
}} ```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,347评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,906评论 18 399
  • 凌晨的天空,变幻莫测,每天景色都不同。无人知道明天会是什么样,今天的也很快会逝去。当下那刻,感受她的美,足够。奔跑...
    梅子Mey阅读 2,118评论 4 3
  • 在外面学习,昨晚回到家。Q滔滔不绝地跟我分享他这几天发现的“高科技”:数码纸,水雾显示屏,防盗设备……说实话,之前...
    GraceQ妈阅读 1,930评论 0 0