java 迴圈變數

來源:互聯網
上載者:User

標籤:語句   ase   輸出   http   pow   預設   system   支援   迭代   

方法:是一段完成特定功能代碼的片段。

格式: [修飾符1 修飾符2 ......]傳回值類型 方法名(形式參數列表){

java語句;.......

}

形式參數:在方法被調用時用於接收外界輸入的資料。

實參:調用方法時實際傳給方法的資料。

傳回值:在方法執行完畢後返回給調用它的環境資料

傳回值類型:事先約定的傳回值資料類型,若無傳回值,則必須給void

java語言中使用調用方法:對象名.方法名(實參列表).

實參的數目,資料類型和次序必須和所調用的方法聲明形式參數列表匹配

return 語句終止方法的運行並指定要傳回型別

java中只有值傳遞

簡單的讀取鍵盤輸入的字串:

Scanner input =new Scanner(Systrm.in);

//String str =input.nextLine();

//String str =input.next();

int str=input.nextInt();

System.out.println(str);

 

順序結構:

JAVA的基本結構就是順序結構,除非特別指明,否則按順序一句一句執行順序結構的簡單演算法,語句與語句之間,框與框之間按從上到下的順序執行,它是若干個依次執行的處理步驟組成。

if單結構:

double i=6*Math.random();//Math.random()產生一個0~1的隨機數

double j=6*Math.random();//6*Math.random()產生 [0,5]

double k=6*Math.random();

iot count=(int)(i+j+j);

if(count>15){

System.out.printlan("運氣不錯");

}

if(count>=10&&count<=15){

System.out.printlan("運氣一班");

}

System.out.println("得了"+count+"分");

 

if else 雙選擇結構:如果條件為true執行一個操作,為false執行另一個操作

若條件運算式為真,執行語句塊1,否則,執行語句塊2.

double r =4*Math.random();//產生隨機數 [0,4]

double are=Math.PI*Math.pow(r,2);//Math中定義的PI值 Math.pow(r.2)也就是r的平方

double cir =2*Math.PI*r;

System.out.println("半徑為:"+r);

System.out.println("面積為:"+are);

System.out.println("周長為:"+cir);

if(are>=cir){

System.out.println("面積大於周長");

}else{

System.out.println("周長大於面積");

}

 

if else if else 多選擇結構

if(布林運算式1){

語句塊1;

}else if(布林運算式2){

語句塊2;

}else if(布林運算式n){

語句塊n;

}else{

語句塊n+1;

}

public class IfTest3 {
 public static void main(String[] args) {
  int age = (int) (100 * Math.random());
  System.out.print("年齡是" + age + ", 屬於");

  if (age < 15) {
   System.out.println("兒童,");
  } else if (age < 25) {
   System.out.println("青年,");
  } else if (age < 45) {
   System.out.println("中年,");
  } else if (age < 65) {
   System.out.println("中老年,");
  } else if (age < 85) {
   System.out.println("老年,");
  } else {
   System.out.println("老壽星,");
  }
 }
}

 

 switch多選擇結構:根據運算式不同執行不同的操作

switch語句:case標籤必須是整數或枚舉,不能是字串。

switch(運算式){//byte,int,char,short和枚舉類型。

  case 值1;//值1必須是常量

  語句序列;

  break;//每個語句都以break結尾

  case 值2;

  語句序列;

  break;

...

default;//可有可無

預設語句;

}

 

switch語句會根據運算式的值匹配的case標籤處開始執行,一直執行到break語句處或者是switch語句的末尾。與case值不匹配,則進入下一語句。

public class vir{

public static void main(String[]args){

char c =‘a‘;

int rand=(int)(26*Math.random());//(int)強制轉換為int類型  產生一個[0,25]的隨機數

char c2=(char)(c+rand);

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

switch(c2){

case=‘a‘://利用case貫穿, 輸出所有的字元直到遇到break

case=‘e‘:

case=‘i‘:

case=‘o‘:

case=‘u‘:

System.out.println("母音");

break;

case=‘y‘:

case=‘w‘:

System.out.println("半母音");

break:

default:

System.out.println("輔音");

}

}

樣本:

System.out.print("請輸入月份:");
   int month =(int)(1+Math.random()*12);
   if(month>0&&month<13){
    switch (month) {
    case 2:
     System.out.println("28天");
     break;
    case 4:
    case 6:
    case 9:
    case 11:
     System.out.println("30天");
     break;
    default:
     System.out.println("31天");
    }

   }else{
    System.out.println ("您輸入的月份不合法!");
   }

 

while迴圈結構;

while語句格式:

while(布林運算式){

迴圈體;

}

1.當迴圈開始時,會計算一次布林運算式的值,若條件為真,執行迴圈體。而對於後來每一次迴圈。都會重新計算一次。

2.語句中應有迴圈趨向於結束的語句,否則死迴圈。

public class Test{

public static void main(String[] args){

int i=0;

int sum=0;

while(i<100){//迴圈條件

sum+=i;//計算總和

i++;

}

System.out.println("sum="+sum);

}

}

 

 

do while 迴圈

do{

迴圈體;

}while(布林運算式);

先執行後判斷,至少執行一次。

public class Test{

public static void main(String[] args){

int i=0;

int sum=0;

do{

sum+=i;

i++;

}while(i<=100);

System.out.println("sum="+sum);

}

}

 while和dowhile的區別

dowhile至少被執行一次。

int a =0;

while(a<0){

System.out.println(a);

i++;

}

System.out.println("******");

a=0;

do{

System.out.println(a);

i++;

}while(a<0);

 

 

for迴圈

1.for迴圈語句支援迭代的一種通用結構

格式:

for(初始運算式;布林運算式;步進){

迴圈體;

}

 

for迴圈會在第一次反覆前進行初始化,然後執行條件測試,而且在每一次反覆的時候,進行步進;

1.初始化迴圈變數的初值

2.條件運算式判斷為布爾類型

3.迭代因子控制迴圈變數的增減

for (初始運算式;布林運算式;迭代因子) {
   迴圈體;
}

public class ForTest {
 public static void main(String args[]) {
  int sum = 0;
  for (int i = 0; i <= 100; i++) {
   sum += i;
  }
  System.out.println("Sum= " + sum);

for(int i=9;i>0;i--){
  System.out.println(i);
 }
 
 for(int i=90;i>0;i-=3){
  System.out.println(i);
 }
 }
} 

 

java 迴圈變數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.