Java-流程控制語句

流程控制語句

順序語句:程序至上而下執行,一條執行完畢後再執行下一條,一直執行到程序末尾;html

選擇語句:判斷語句,根據條件來選擇到底執行哪一個語句;java

循環語句:根據條件來判斷執行的次數。算法

 

一、 控制檯輸入

執行步驟:先有鍵盤,鏈接設備、安裝驅動,輸入數字dom

import java.util.Scanner;  //該代碼必須在package下一行,如無package聲明,可在首行函數

new Scanner(System.in).nextInt()         //獲得用戶輸入的整數spa

new Scanmer (System.in).nextDouble()    //獲得用戶輸入的浮點數code

new Scanner(System.in).next()           //獲得用戶輸入的字符串,空格結束htm

new Scanner(System.in).nextLine()       //獲得用戶輸入的字符串,換行結束blog

範例:ci

import java.util.Scanner;   //導包(裝鍵盤驅動),Scanner能夠變成*

public class demo2{

         public static void main(String[] args) {  

                  Scanner scan = new Scanner(System.in);  //鏈接鍵盤,scan是一個名字,能夠隨意取

                  System.out.println("請輸入整數");

                  int num1 = scan.nextInt();

                  System.out.println("你輸入的整數是"+num1);

                  System.out.println("請輸入小數");

                  double num2 = scan.nextDouble();

                  System.out.println("你輸入的小數是"+num2);

         }

}

import java.util.Scanner;

public class demo4{

         public static void main(String[] args) {

                  System.out.println("東軟跨境電商平臺");

                  System.out.println("1.製造商管理");

                  System.out.println("2.商品管理");

                  System.out.println("請輸入你的選擇");

                  Scanner scan = new Scanner(System.in);

                  int a = scan.nextInt();

                  if(a==1){

                          System.out.println("進入東軟跨境電商平臺");

                  }else if(a==2){

                          System.out.println("進入1.製造商管理");

                  }else if(a==3){

                          System.out.println("進入2.商品管理");

                  }else{

                          System.out.println("退出");

                  }

         }

}

產生隨機數

    隨機數:在產生過程當中是按照部分機率隨機產生的,其結果是不可預測的;

    僞隨機數:計算機中的隨機函數是按照必定算法模擬產生的,所以成爲僞隨機數

import java.util.Random;  //該代碼必須在package下一行,如無package聲明,可在首行

多行import能夠依次編寫,import代碼沒有必須順序

產生隨機代碼:new Randow().nextInt(正整數);

括號中的數字表明產生隨機數的範圍,如爲10,則產生0~9之間的隨機整數

import java.util.Random;

public class demo40{

         public static void main(String[] args) {

                  int a = new Random().nextInt(10);

                  System.out.println("隨機數爲"+a);

         }

}   //隨機數爲2

 

二、 選擇語句if

1).If語句語法格式1

If(判斷條件){

    語句;

}

判斷語句能夠是一個boolean值,也能夠是一個表達式(結果也必須是boolean值)

public class demo30{

         public static void main(String[] args) {

                  int a = 3 ;

                  if (a>10){

                          System.out.println(a);

                  }

                  System.out.println("a<10");  

         }

}

2).If語句語法格式2

If(判斷條件){

成立時的語句;

}

Else{

不成立時的語句;

}

public class demo31{

         public static void main(String[] args) {

                  int a = 3 ;

                  if (a%2==1){

                          System.out.println("奇數");

                  }

                  else{

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

                  }

         }

}

System.out.println((a%2==1)?"奇數":"偶數");    以上if語句等同於此三目運算符

If語句中的花括號能夠省略,可是不建議省略

3).If語句語法格式3:

If(判斷條件){

成立時的語句;

}

Else if{

成立時的語句1;

}

Else {

不成立時的語句;

}

public class demo1{

         public static void main(String[] args) {

                  int a = 3 ;    

                  if(a==1){

                           System.out.println("a的值等於1");

                  }else if(a==2){

                          System.out.println("a的值等於2");

                  }else if(a==3){

                          System.out.println("a的值等於3");

                  }else{

                          System.out.println("其餘數字");

                  }

         }

}

 

三、 選擇語句switch

語法格式

Switch(表達式|變量){

   Case  值1:

語句;

[ break; ]

   Case  值2:

語句;

[ break; ]

 Default:

          語句;

}

import java.util.Scanner;

public class demo5{

         public static void main(String[] args) {

                  System.out.println("東軟跨境電商平臺");

                  System.out.println("1.製造商管理");

                  System.out.println("2.商品管理");

                  System.out.println("請輸入你的選擇");

                  Scanner scan = new Scanner(System.in);

                  int a = scan.nextInt();

                  switch(a){

                  case 1:

                          System.out.println("進入東軟跨境電商平臺");

                          break;

                  case 2:

                          System.out.println("進入1.製造商管理");

                          break;

                  case 3:

                          System.out.println("進入2.商品管理");

                          break;

                  default:

                          System.out.println("退出");

                  }

         }

}

說明:

a).Switch支持的類型:byte、int、short、char、String、Byte、Integer、Character、枚舉,注意不支持boolean,包括返回boolean類型的表達式

b).case後面的值不可重複(和unicode轉義字符集重複也不行),不一樣進制的相同值也不行

c).case後面的值類型不必定要統一,但需是支持的類型,且能夠相互轉換的

d).default的位置能夠改變,但若不在末尾,建議給default增長一個break;

e).case後面的值必須是常量,不能夠是變量

public class demo6{

         public static void main(String[] args) {

                  char input = '男';   //採用的是unicode轉義字符集,\u7537

                  switch(input){

                  case 'a':

                          System.out.println("進入東軟跨境電商平臺");

                          break;

                  case '男':

                          System.out.println("進入1.製造商管理");

                          break;

                  case 999:

                          System.out.println("進入2.商品管理");

                          break;

                  default:

                          System.out.println("退出");

                  }

         }

}   //運行正常

public class demo7{

         public static void main(String[] args) {

                  char input = 86;

                  switch(input/10){

                  case 10:

                          System.out.println("滿分");

                          break;

                  case 9:

                          System.out.println("優秀");

                          break;

                  default:

                          System.out.println("差");

                          break;

                  case 8:

                          System.out.println("良好");

                          break;

                  }

         }

}

public class demo8{

         public static void main(String[] args) {

                  char input = 95;

                  final int Y = 9;  //常量的表示,在前面加上final,且名字建議所有爲大寫

                  switch(input/10){

                  case 10:

                          System.out.println("滿分");

                          break;

                  case Y:

                          System.out.println("優秀");

                          break;

                  default:

                          System.out.println("差");

                          break;

                  case 8:

                          System.out.println("良好");

                          break;

                  }

         }

}       //返回 優秀

 

四、 循環語句while

語法格式:

while(循環判斷條件){

語句;

}

public class demo11{

         public static void main(String[] args) {

                  int a = 1;

                  int sum = 0;

                  while(a<=100){

                          sum += a;

                          a++;

                  }

                  System.out.println("結果爲"+sum);

         }

}     //結果爲5050

 

五、 循環語句do…while

語法格式:

do{

語句;

}while(循環判斷條件);

public class demo13{

         public static void main(String[] args) {

                  int a = 1;

                  int sum = 0;

                  do{

                      sum += a;

                          a++;

                  }while(a<=100);

                  System.out.println("結果爲"+sum);

         }

}

While和do…while的區別:

While是先判斷,符合條件才執行語句;Do…while不論是否符合都先執行語句,再進行判斷

Do…while最少執行一次,while能夠一次都不執行;

語法上,Do…while後面必須加上分號,while後面能夠不加分號。

練習1:打印出100之內全部的奇數

public class demo14{

         public static void main(String[] args) {

                  int a = 1;

                  while (a<=100){

                  switch(a%2){

                  case 1:

                          System.out.print(a);

                          break;

                  case 0:

                          System.out.print(",");

                  }a++;

                  }

         }

}

練習2:求10之內奇數之積

public class demo15{

         public static void main(String[] args) {

                  int a = 1 ;

                  int sum = 1 ;

                  while (a<=10){

                  switch(a%2){

                  case 1:

                          sum *= a;

                          break;

                  }

                  a++;

                  }

                  System.out.print(sum);

         }

}

 

六、 循環語句for

語法格式

for (初始化部分; 循環條件判斷部分; 迭代部分){

         執行語句塊;

}

範例

public class demo20{

         public static void main(String[] args) {

                  int sum = 0 ;

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

                          sum +=a;

                  }

                  System.out.print(sum);

         }

}

For循環中的3個部分均可以省略,可是格式不能變

For循環中的3個部分能夠包含多個變量,不僅是一個變量

public class demo21{

         public static void main(String[] args) {

                  int a = 1 ;

                  int sum = 0 ;

                  for(;;){

                          if(a<=100){

                                   sum +=a;

                                   a++;

                          }else{

                                   break;

                                   }

                  }System.out.print(sum);

         }

}

public class demo29{

         public static void main(String[] args) {

                  int sum = 0 ;

                  for(int a=1;a<=100;sum+=a,a++){

                  }

                  System.out.println(sum);

         }

}

For的判斷條件能夠是任意的基本數據類型之一,包括boolean

public class demo40{

         public static void main(String[] args) {

                  boolean flag = false;

                  for(;!flag;){

                          System.out.println("a");

                  }

         }

}

For語句中的花括號能夠省略,但不建議省略

在for循環中,初始值部分和迭代部分能夠是多個表達式,表達式之間用逗號分隔

public class demo25{

         public static void main(String[] args) {

                  for(int a=1,b=0;a<=10;a++,b++){

                          System.out.println("hello");

                  }

         }

}

public class demo26{

         public static void main(String[] args) {

                  for(int a=1,b=10;a<=b;a++,b--){

                          System.out.println(a*b);

                  }

         }

}

For的判斷條件能夠是語句,不僅是表達式

public class demo27{

         public static void main(String[] args) {

                  int i = 0 ;

                  for(System.out.print("初始值"),System.out.println(i);i<=3;System.out.println(i++)){

                  }

         }

}

 

七、 做用域

public class demo30{

         public static void main(String[] args) {

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

                          int sum = 0 ;

                          sum += a;

                  }

                  System.out.println(sum);

         }

}      //報錯,找不到第七行的變量sum

 

八、 嵌套

難點:for循環的嵌套

public class demo31{

         public static void main(String[] args) {

                  for(int a=1;a<=5;a++){       //第一個for循環 負責行數

                          for(int b=1;b<=8;b++){   //第二個for循環 負責每行的個數

                                   System.out.print("*");

                          }

                          System.out.println();

                  }

         }

}

 

 

九、 Break和continue語句

在使用循環語句時,只有循環條件爲false時,纔可結束循環。有時,咱們想提早終端循環,那就可使用break或continue語句,

Break語句能夠迫使程序中斷,跳出循環。Break語句用於終止最近的封閉循環或它所在的switch語句,控制傳遞給種植語句後面的語句。

public class demo32{

         public static void main(String[] args) {

                  for(int a=1;a<=5;a++){       //負責行數

                          if(a==4){

                                   break;

                          }

                          System.out.println(a);

                  }

         }

}

continue語句語句將控制權傳遞給它所在的封閉迭代語句的下一次迭代。

也就是說,continue語句能夠跳出本次循環,繼續執行下一次循環。

continue語句只能出如今循環語句while、do…while、for中

public class demo33{

         public static void main(String[] args) {

                  for(int a=1;a<=5;a++){       //負責行數

                          if(a==4){

                                   continue;

                          }

                          System.out.println(a);

                  }

         }

}

 

十、 死循環

         靠自身控制沒法終止的程序稱爲「死循環」

  

 

課後練習題:

練習1:鍵盤輸入一個月份,判斷當月多少天?(2月28天)

import java.util.Scanner;

public class demo36{

         public static void main(String[] args) {

                  Scanner scan = new Scanner(System.in);

                  System.out.println("請輸入月份");

                  int month = scan.nextInt();

                          switch(month){

                          case 1:

                          case 3:                     

                          case 5:                              

                          case 7:                               

                          case 8:                                       

                          case 10:

                          case 12:

                                   System.out.println("你輸入的月份有31天");

                                   break;

                          case 2:

                                   System.out.println("你輸入的月份有28天");

                                   break;

                          case 4:

                          case 6:                     

                          case 9:                              

                          case 11:                            

                                   System.out.println("你輸入的月份有30天");

                                   break;

                          default:

                                   System.out.println("你輸入的月份有誤");

                                   break;

                  }

         }

}

練習2:循環獲得用戶從控制檯輸入的5個整數,並計算出它們之和

import java.util.Scanner;

public class demo37{

         public static void main(String[] args) {

                  int sum = 0 ;

                  for(int a=1 ; a<=5 ; a++){

                          Scanner scan = new Scanner(System.in);

                          System.out.println("請輸入整數");

                          int x = scan.nextInt();

                          sum += x;

                  }System.out.println("總數爲"+sum);    

         }

}

 

 

做者:kerwin-chyl

文章連接:https:////www.cnblogs.com/kerwin-chyl

本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利

相關文章
相關標籤/搜索