條件語句與循環(huán)語句

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?條件語句與循環(huán)語句

1.順序結(jié)構(gòu)的程序語句只能被執(zhí)行一次。如果您想要同樣的操作執(zhí)行多次,,就需要使用循環(huán)結(jié)構(gòu)。

Java中有三種主要的循環(huán)結(jié)構(gòu):

while循環(huán)

do…while循環(huán)

for循環(huán)



while循環(huán)

代碼:

public class While{

public? static void main(String[] args){

//求1-100的和,求1-100的偶數(shù)和,求1-100的奇數(shù)和

int sum= 0;

int a= 1;

while(a<=100){

sum+=a;

a++;

}

System.out.println(sum);

//求1-100的偶數(shù)和

int sun1=0;

int x=1;

while(x<=100){

x++;

if(x%2==0){

sun1+=x;

}

}

System.out.println(sun1);

//求1-100的奇數(shù)和

int sun2=0;

int y=1;

while(y<=100){

y++;

if(y%2!=0){

sun2+=y;

}

}

System.out.println(sun2);

//1-50當(dāng)中能被4整除的數(shù)的和

int sum2=0;

int k= 1;

while(k<=50){

k++;

if(k%4==0)

? sum2+=k;

}

System.out.println(sum2);

//求出 1-100當(dāng)中既能被3整除又能被5整除還能被2整除的和

int sum3=0;

int j= 1;

while(j<=100){

j++;

if(j%5==0&&j%3==0&&j%2==0){

sum3+=j;

}

}

System.out.println(sum3);

//求出 1-100當(dāng)中能被3整除或者能被5整除或者能被2整除的和

int sum4=0;

int b= 1;

while(b<=100){

b++;

if(b%5==0||b%3==0||b%2==0){

sum4+=b;

}

}

System.out.println(sum4);

//請(qǐng)找出[100,300]之間能被5整除的所有數(shù),每行輸出8個(gè)數(shù)

int count=0;

int c=100;

while(c<=300){

c++;

if(c%5==0){

System.out.print(c+" ");

count++;

if(count%8==0){

System.out.println();

}

}

}

}

}

do…while循環(huán)

代碼:

public class Test { 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 );

? }

}

public class Text {

public static void main(String[] args) {

int sum1 =0;

? ? ? ? int a =1;

? ? ? ? do {

sum1 += a;

? ? ? ? ? ? a++;

? ? ? ? }while (a <=100);

? ? ? ? System.out.println(sum1);

? ? }

}

for循環(huán)

代碼:

public class Text3 {

? ? //5的階乘 5!=5*4*3*2*1

? ? public static void main(String[] args) {

? ? ? int a = 10 ;

? ? ? for(int b = 9;b>=1;b--){

? ? ? ? ? a*=b;

? ? ? }

? ? ? ? System.out.println(a);

? ? //九九乘法表

? ? ? ? for(int x=1;x<=9;x++){

? ? ? ? ? ? for(int y = 1;y<=x;y++){

? ? ? ? ? ? ? ? System.out.print(y+"*"+x+"="+(x*y)+"\t");

? ? ? ? ? ? }

? ? ? ? ? ? System.out.println();

? ? ? ? }

? ? ? // 求出 1-100當(dāng)中既能被3整除又能被5整除還能被2整除的和

? ? ? ? int sum=0;

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

? ? ? ? ? ? if(i%3==0&&i%5==0&&i%2==0){

? ? ? ? ? ? ? ? sum=sum+i;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? System.out.println(sum);

? ? ? ? //求出 1-100當(dāng)中能被3整除或者能被5整除或者能被2整除的和

? ? ? ? int sum1=0;

? ? ? ? for(int j=1;j<=100;j++){

? ? ? ? ? ? if(j%3==0||j%5==0||j%2==0){

? ? ? ? ? ? ? ? sum1=sum1+j;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? System.out.println(sum1);

? ? ? ? //請(qǐng)找出[100,300]之間能被5整除的所有數(shù),每行輸出8個(gè)數(shù)

? ? ? ? int count=0;

? ? ? ? for(int k= 100;k<=300;k++){

? ? ? ? ? ? if(k%5==0){

? ? ? ? ? ? ? ? if(count==0){

? ? ? ? ? ? ? ? ? ? System.out.println(k);

? ? ? ? ? ? ? ? }else{

? ? ? ? ? ? ? ? ? ? System.out.print(","+k);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? count++;

? ? ? ? ? ? ? if(count%8==0){

? ? ? ? ? ? ? ? ? System.out.println();

? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? }

? ? }

2.if...else if...else 語句

if 語句后面可以跟 else if…else 語句,這種語句可以檢測(cè)到多種可能的情況。

使用 if,else if,else 語句的時(shí)候,需要注意下面幾點(diǎn):

if 語句至多有 1 個(gè) else 語句,else 語句在所有的 else if 語句之后。

if 語句可以有若干個(gè) else if 語句,它們必須在 else 語句之前。

一旦其中一個(gè) else if 語句檢測(cè)為 true,其他的 else if 以及 else 語句都將跳過執(zhí)行。

代碼:

public class Test { 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("這是 else 語句");

? ? ? }??

?}

}

if...else語句

if 語句后面可以跟 else 語句,當(dāng) if 語句的布爾表達(dá)式值為 false 時(shí),else 語句塊會(huì)被執(zhí)行。

代碼:

public class Test {

? public static void main(String args[]){? ? ? int x = 30;

? ? ? if( x < 20 ){? ? ? ? System.out.print("這是 if 語句");

? ? ? }else{? ? ? ? System.out.print("這是 else 語句");

? ? ? }? }}

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

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

  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子...
    趙宇_阿特奇閱讀 2,083評(píng)論 0 2
  • 50道經(jīng)典Java編程練習(xí)題,將數(shù)學(xué)思維運(yùn)用到編程中來。抱歉哈找不到文章的原貼了,有冒犯的麻煩知會(huì)聲哈~ 1.指數(shù)...
    OSET我要編程閱讀 7,290評(píng)論 0 9
  • 1 順序語句 語句:使用分號(hào)分隔的代碼稱作為一個(gè)語句。 注意:沒有寫任何代碼只是一個(gè)分號(hào)的時(shí)候,也是一條語句,...
    哈哈哎呦喂閱讀 480評(píng)論 0 0
  • Day01 class 例子{ public static void main(String[] args){ ...
    周書達(dá)閱讀 1,217評(píng)論 0 0
  • 寫在前面: hi~同學(xué)們大家好! 今天小編要跟大家分享 對(duì)于學(xué)生黨或上班族, 困擾大家的一大難題... 腰部愛長贅...
    慧診君閱讀 137評(píng)論 0 0

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