12月26日學(xué)習(xí)內(nèi)容

While循環(huán)

例子:

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循環(huán)
例子:

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 語句由一個(gè)布爾表達(dá)式后跟一個(gè)或多個(gè)語句組成。

if 語句的語法是:

if(Boolean_expression){ //Statements will execute if the Boolean expression is true}```
如果布爾表達(dá)式的值為 true,那么代碼里面的塊 if 語句將被執(zhí)行。如果不是 true,在 if 語句大括號后結(jié)束后的第一套代碼將被執(zhí)行。
####例子

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 語句后面可以跟一個(gè)可選的 else 語句,當(dāng)布爾表達(dá)式為 false,語句被執(zhí)行。

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");}
} } ```

當(dāng)使用 if , else if , else 語句時(shí)有幾點(diǎn)要牢記。

一個(gè) if 語句可以有0個(gè)或一個(gè) else 語句 且它必須在 else if 語句的之后。
一個(gè) if 語句 可以有0個(gè)或多個(gè) else if 語句且它們必須在 else 語句之前。
一旦 else if 語句成功, 余下 else if 語句或 else 語句都不會被測試執(zhí)行。

例子

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 語句,這意味著你可以在另一個(gè) if 或 else if 語句中使用一個(gè) 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");
}
}
} } ```

作業(yè)練習(xí),建立一個(gè)數(shù)組,把元素從小到大排列

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]+" ");}
}} ```
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,602評論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,734評論 18 399
  • 凌晨的天空,變幻莫測,每天景色都不同。無人知道明天會是什么樣,今天的也很快會逝去。當(dāng)下那刻,感受她的美,足夠。奔跑...
    梅子Mey閱讀 317評論 4 3
  • 在外面學(xué)習(xí),昨晚回到家。Q滔滔不絕地跟我分享他這幾天發(fā)現(xiàn)的“高科技”:數(shù)碼紙,水霧顯示屏,防盜設(shè)備……說實(shí)話,之前...
    GraceQ媽閱讀 330評論 0 0

友情鏈接更多精彩內(nèi)容