Java 第四章 if循環那些


4-1  條件語句if

語法 if(條件){

條件成立時 執行的代碼

}

 

package com.mooc;

 

public class logiccontent01 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int one=20;

if((one%2==0)){

System.out.println("one是偶數");

}

 

}

 

}

 

4-2  if  else

語法   if(條件的布爾表達式){

代碼快1

}else{

代碼快2

}

 

package com.mooc;

 

public class logiccontent02 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int  age=25;

if(age>=18){

System.out.println("成年");

}else{

System.out.println("未成年");

}

 

}

 

}

 

4-3  多重if

多重if語句,在條件1不滿足的情況下,纔會進行情況2 的判斷;

在前面的條件均不成立的條件下,纔會執行else的 代碼。

語法

If(條件1{

代碼快1

}else if{

代碼快2

}else{

代碼快3

}

 

 

package com.mooc;

 

public class logiccontent03 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int age=25;

if(age>60){

System.out.println("老年");

}else if((age>=40)&&(age<=60)){

System.out.println("中年");

}else  if((age<40)&&(age>=18)){

System.out.println("少年");

}else {

System.out.println("童年");

}

 

}

 

}

 

 

4-4   嵌套if

If(條件1{

If(條件2{

代碼快1

}else{

代碼快2

}

}else{

代碼快3

}

 

 

package com.mooc;

 

public class logiccontent04 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int  score=94;

String  sex ="女";

if(score>80){

if(sex =="女"){

System.out.println("可進入女子組決賽");

}else{

System.out.println("可進入男子組決賽");

}

}else{

System.out.println("不可進入決賽");

}

 

}

 

}

 

 

4-6   switch  語句

語法

Switch(表達式){

Case 1

執行代碼快1

Break;

Case  n:

執行代碼快n

break;

Default:

默認執行代碼

}

 

語法

Switch(表達式){

Case值1:

執行代碼1

Break;

Case值n:

執行代碼快n

Break;

      Default:

默認執行的 代碼快

}

注意

1.switch 後面小括號表達式的值 必須是  整型或字符型;

2.Case 後面值 可以是常量,如1,2;

也可以是常量表達式  2+2;

           3.default 可以出現任意位置  也可以省略。

 

 

package com.mooc;

 

public class logiccontent05 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

char  today='日';

switch (today){

     case  '一':

     case  '三':

     case  '五':

      System.out.println("吃包子");

      break;

     case  '二':

     case  '四':

     case   '六':

      System.out.println("吃油條");

      break;

    default:

     System.out.println("吃主席套餐");

     

      

}

 

4-7  循環語句 之 while

語法

While(判斷條件){

循環操作

}

 

package com.mooc;

 

public class logiccontent06 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int  i=1;

while(i<=5){

System.out.println(i);

i++;

}

 

}

 

}

 

 

 

4-8    do   while

do{

循環操作

}  while  (判斷條件);

 

 

 

package com.mooc;

 

public class logiccontent08 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int  sum=0;

int num=2;

do{

sum=sum+num;

num=num+2;

}while(num<=50);

System.out.println("50以內的偶數之和爲爲:"+sum);

}

}

 

 

 

4-9   循環 for

For(循環變量初始化;循環條件;循環變量變化){

循環操作

}

注意

1. for關鍵字括號裏面的三個表達式必須用;分開,三個表達式都可以省略,但是;不能省略;

2. 省略循環條件 死循環,此時可以用break 挑出循環

3. 省略 循環變量變化,可以在循環體 裏面進行 循環變量的變化

4. For循環變量初始化和循環變量變化部分,可以用,  同時初始化多個;

5. 循環條件部分 可以用 邏輯運算組合的表達式  表示複雜判斷條件, 但一定注意 運算優先級;

 

package com.mooc;

 

public class logiccontent09 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

int sum=0;

for(int i=1;i<=100;i++){

if(i%3!=0){

sum=sum+i;

}

}

System.out.println("1到100之間不能被3整除的數之和爲:"+sum);

 

}

 

}

 

 

4-11  break語句

package com.mooc;

 

public class logiccontent10 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

     int   sum=0;

     for(int i=1;i<=10;i++){

      sum=sum+i;

      if(sum>20){

      System.out.println("當前的累加值爲:"+sum);

      break;

      }

     }

 

}

 

}

 

 

4-12 循環跳轉語句 continue

作用是 跳過循環體裏面剩餘的語句執行下一次 循環。

例如

Forint i=1;i<=10;i++{

If(i%2!=0){

Continue;

}

System.out.println(i);

}

運行結果

2

4

6

8

10

 

 

 

 

 

 

另外一個例子

package com.mooc;

 

public class logiccontent11 {

public static void main(String[] args) {

int  sum=0;

for(int i=1;i<=10;i++){

if(i%2!=0){

continue;

}

sum=sum+i;

}

System.out.println("1到10之間的所有偶數的和爲:"+sum);

}

 

}

 

 

4-14   多重循環

三種循環可以自身嵌套 也可以互相嵌套

最常見的就是二重嵌套。

package com.mooc;

 

public class logiccontent12 {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("打印直角三角形");

for(int i=1;i<=3;i++){

for(int j=1;j<=i;j++){

System.out.print("*");

}

System.out.println();

}

 

}

 

}

 

打印直角三角形

*

**

 

 

 

 

總結一下學習的條件判斷語句

If

If else

多重if

嵌套if

 

Switch

 

循環  while   

      do while

     

      For

 

 

Break

Continue

 

 

多重嵌套  隨便套 只要你岺的清,算得出來就行。

 

 

 

4-15  編程練習

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章