Java語言基礎選擇結構_第1頁
Java語言基礎選擇結構_第2頁
Java語言基礎選擇結構_第3頁
Java語言基礎選擇結構_第4頁
Java語言基礎選擇結構_第5頁
已閱讀5頁,還剩20頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、第二章 Java 語言基礎(選擇結構)新課引入public class Test1 public static void main(String args ) int x,y,z; x=1; y=2; z=x+y; System.out.println( x+y= +z); import java.io.*;public class Test2 public static void main(String args ) throws IOException int x,y,z; String s; BufferedReader r=new BufferedReader( new InputSt

2、reamReader(System.in); System.out.println(”enter x:”); s=r.readLine(); x=Integer.parseInt(s); /將該字符串轉換為整型 System.out.println(”enter y:”); s=r.readLine(); y=Integer.parseInt(s); /將該字符串轉換為整型 z=x+y; System.out.println( x+y= +z); 一、結構化編程流程控制語句用于控制程序中各語句的執行順序。Java提供的流程控制語句有順序結構、選擇結構、循環結構、轉移等。if(條件表達式) 語句

3、塊1else 語句塊2 if-else 語句流程圖條件語句塊1truefalse語句塊2二、if 條件語句說明:if后的表達式是判定條件。一般為邏輯表達式或關系表達式。語句中if和else屬于同一個條件語句,else子句不能作為語句單獨使用,必須與if配對使用。語句序列可以含一條或多條執行語句。當有多條執行語句時必須用“ ”,將幾個語句括起來成為一條復合語句,否則,只執行第一條語句。else及后面的語句序列可以省略。在if子句末不能加分號“ ;”if與else的配對原則二、if 條件語句例如:if(ab) System.out.println(”ab”);二、if 條件語句例如:if(scor

4、e=60) System.out.println(”你及格了”);else System.out.println(”你沒有及格”);二、if 條件語句例1:判斷一個數是否為奇數,如果為奇數則輸出,否則不予理睬。public class IsOdd public static void main(String args) int in =11, mod;mod = in%2;if(mod=1) System.out.println(數字 + in + 為奇數);二、if 條件語句例2:要求在例1的執行過程中,如果用戶輸入偶數必須給出提示。二、if 條件語句if ( 條件表達式 )語句塊1;els

5、e語句塊2;import java.io.*;public class IsOdd2 public static void main(String args ) throws IOException int x; String s; BufferedReader r=new BufferedReader (new InputStreamReader(System.in); System.out.println(”enter an integer:”); s=r.readLine(); x=Integer.parseInt(s); if(x%2=1) System.out.println(你輸入

6、的數字 + x+ 為奇數); else System.out.println(你輸入的數字 + x + 為偶數); 例3:import java.io.*;public class Test public static void main(String args) throws IOException char ch; System.out.print(請輸入一個字符:); ch = (char)System.in.read( ); / 從鍵盤輸入一個字符 if (ch=0 & ch=85 & score=60 & score=0 & score60) System.out.println(不

7、通過); else System.out.println(成績有誤);二、ifelse ifelse多選擇語句if(表達式1)if(表達式2)語句序列1else語句序列2elseif(表達式3)語句序列3else語句序列4二、ifelse ifelse多選擇語句例如:if(Score60) System.out.println(不及格);else if(Score80) System.out.println(及格); else if(Score90)System.out.println(良好); else System.out.println(優秀);二、ifelse ifelse多選擇語句例

8、4:根據年份和月份輸出每月的天數。二、ifelse ifelse多選擇語句public class IfTest public static int dayOfMonth(int year,int month) if(month=2) if(year%4=0&(year%400=0|year%100!=0) return 29; else return 28; else if(month=4|month=6|month=9|month=11) return 30; else return 31; public static void main(String args) int days=day

9、OfMonth(2008,2); System.out.println(days of February,2008 is: +days); 二、ifelse ifelse多選擇語句三、switch 語句switch(表達式) case 值1:語句塊 1 ;case 值2:語句塊2 ;default:語句塊n;switch 的常量和表達式可以是整型、字符型及byte型任何兩個case常量值不可以有相同的值。只能對等式進行測試(即表達式的值是否等于值1、值2.,根據表達式取值的不同轉向不同的分支。每個case分支中的語句塊無須用花括號括起來。每個case分支都只是入口點可在case語句塊中加入br

10、eak 語句,可立即轉出switch語句(不再走后面case流程)。三、switch 語句例5:import java.io.*;public class Test public static void main(String args) throws IOException char ch ; System.out.print(請輸入成績(字符):); ch = (char)System.in.read() ; / 從鍵盤輸入一個字符 switch (ch) case A : System.out.println(85100); break ; case B : System.out.pri

11、ntln(6084); break ; case C : System.out.println(059); break ; default : System.out.println(輸入有誤); 例6:import java.io.*;public class Test public static void main(String args) throws IOException char ch ; System.out.print(請輸入成績(字符):); ch = (char)System.in.read() ; / 從鍵盤輸入一個字符 switch (ch) case a : case

12、A : System.out.println(85100); break ; case b : case B : System.out.println(6084); break ; case c : case C : System.out.println(059); break ; default : System.out.println(輸入有誤); 練習練習1:若a、b、c1、c2、x、y均是整型變量,正確的switch語句是 。(A)swich(a+b); (B)switch(a*a+b*b) case 1:y=a+b;break; case 3: case 0:y=a-b;break;

13、 case 1:y=a+b;break; case 3:y=b-a;break; (C)switch a (D)switch(a-b) case 3: case c1:y=a-b;break; case 4:x=a+b;break; case c2:x=a*d;break; case 10: default:x=a+b; case 11:y=a-b;break; default:y=a*b;break; 練習2:分析程序運行結果。public class s1 public static void main(String args) int x=1; switch(x) case 0: System.out.println(first);break; case 1: System.out.println(second); case 2: System.out.println(third); break; 練習3:分析程序運行結果

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論