選擇語句+循環語句作業
一、 填空題
- Java中有兩種類型的選擇結構的控制語句,分別是if語句和 switch 。
- 在Java JDK1.7之前,switch只能支持byte、short、char、int或者其對應的封裝類以及Enum類型。在JDK1.7中又加入了 String 類型。
- for循環的語法格式是for (表達式1;表達式2;表達式3) {循環體},其中在整個循環過程中只執行一次的部分是 表達式1 。
- 在循環結構中,如果想跳出循環體,結束整個循環結構可以使用 break 語句。
- ___________continue__語句用在循環語句體中,用於終止某次循環過程,即跳過循環體中尚未執行的語句,接着進行下一次是否執行循環的判定。即只結束本次循環,而不是終止整個循環的執行。
二、 選擇題
1. |
以下代碼的執行結果是( B )。(選擇一項) |
|
|
boolean m = false; if(m = false){ System.out.println("false"); }else{ System.out.println("true"); } |
|
|
|
|
|
A. |
false |
|
B. |
true |
|
C. |
編譯錯誤 |
|
D. |
無結果 |
2. |
分析如下Java代碼,編譯運行的輸出結果是( B )。(選擇一項) |
|
|
public static void main(String[ ] args) { boolean a=true; boolean b=false; if (!(a&&b)) { System.out.print("!(a&&b)"); }else if (!(a||b)) { System.out.println("!(a||b)"); }else { System.out.println("ab"); } } |
|
|
|
|
|
A |
!(a&&b) |
|
B. |
!(a||b) |
|
C. |
ab |
|
D. |
!(a||b)ab |
3. |
下列選項中關於變量x的定義,( BD )可使以下switch語句編譯通過。(選擇二項) |
|
|
switch(x) { case 100 : System.out.println("One hundred"); break; case 200 : System.out.println("Two hundred"); break; case 300 : System.out.println( "Three hundred"); break; default : System.out.println( "default"); } |
|
|
|
|
|
A |
double x = 100; |
|
B. |
char x = 100; |
|
C. |
String x = "100"; |
|
D. |
int x = 100; |
4. |
閱讀下列文件定入的Java代碼,其執行結果是( D )。(選擇一項) |
|
|
public class Test { public static void main(String[] args) { char ch = 'c'; switch (ch) { case 'a': System.out.print("a"); break; case 'b': System.out.print("ab"); case 'c': System.out.print("c"); default: System.out.print("d"); } } } |
|
|
|
|
|
A |
a |
|
B. |
b |
|
C. |
c |
|
D. |
cd |
5. |
以下Java程序編譯運行后的輸出結果是( B )。(選擇一項) |
|
|
public class Test { public static void main(String[] args) { int i = 0, sum = 0; while (i <= 10) { sum += i;//sum=sum+i i++; } System.out.println(sum); } } |
|
|
|
|
|
A |
0 |
|
B. |
55 |
|
C. |
50 |
|
D. |
36 |
6. |
以下四個選項中和下面代碼功能相同的是( B )。(選擇一項) |
|
|
int i = 1; int sum = 0; while (i <= 100) { if (i % 2 == 0) sum = sum + i; i++; } |
|
|
|
|
|
A |
for (int x =1; x<=100;x++){ sum=sum+x;}//1-100的和 |
|
B. |
for (int x =0; x<=100;x+=2){ sum=sum+x;}//1-100之間偶數的和 |
|
C. |
for (int x =1; x<=100;x+=2){ sum=sum+x;}//1-100之間奇數的和 |
|
D. |
上述全對 |
7. |
以下do-while循環代碼的執行結果是( A )。(選擇一項) |
|
|
int a=0; int c=0; do{ --c; a=a-1; }while(a>0); System.out.println(a+" "+c); |
|
|
|
|
|
A. |
-1 -1 |
|
B. |
死循環 |
|
C. |
-1 -2 |
|
D. |
-1 0 |
8. |
while循環和do-while循環的區別是( D )。(選擇一項) |
|
|
|
|
|
A. |
沒有區別,這兩個結構在任何情況下效果一樣 |
|
B. |
while循環比do-while循環執行效率高 |
|
C. |
while循環是先循環后判斷,所以循環體至少被執行一次 |
|
D. |
do-while循環是先循環后判斷,所以循環體至少被執行一次 |
9. |
在Java中有如下代碼,則編譯運行該類的輸出結果是( D )。(選擇一項) |
|
|
public static void main(String[ ] args) { for(int i=0;i<10;i++){ if (i%2!=0) return;//結束方法 System.out.print(i); } } |
|
|
|
|
|
A |
13578 |
|
B. |
02468 |
|
C. |
0123456789 |
|
D. |
0 |
10. |
下面程序執行的結果是在屏幕上打印( B )次Java基礎班。(選擇一項) |
|
|
for(int i=1;i<=10;i++){ if (i<5) continue; System.out.println("Java基礎班"); } |
|
|
|
|
|
A |
5 |
|
B. |
6 |
|
C. |
7 |
|
D. |
8 |
三、 判斷題(共20個題目,總計10分)
- if語句的條件表達式的結果都必須是boolean值。( √ )
- switch選擇語句是多分支選擇語句,只能處理等值條件判斷的情況,表達式可以是int類型、char類型,但不能是double,float類型。( √ )
- while循環結構的特點是先循環再判斷,循環體至少執行一次。( × )
- for循環的語法格式是for (表達式1;表達式2;表達式3) {循環體},其中三個表達式都可以省略。( × )
- break語句可以出現在switch語句和循環語句中。( √ )
- continue語句可以出現在switch語句和循環語句中。( × )
四、 簡答題
- if多分支語句和switch語句的異同之處
相同點:都能進行多分支情況的處理
不同點:if語句的判斷條件大部分都是bool類型,switch語句是用(類似)枚舉的方式來標識各個分支的
- while和do-while語句的異同之處
相同點:都是循環結構
不同點:while語句是先判斷后執行
while循環是先判斷條件是否成立,然后決定是否執行循環體
do-while語句是先執行后判斷,
do-while循環至少執行一次循環體
- break和continue語句的作用
break:終止循環
continue:終止本次循環,繼續下次循環
五、 編碼題
- 輸入一個數,判斷是奇數還是偶數
import java.util.Scanner;//導包 class Take1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in);//創建鍵盤錄入對象 System.out.print("請輸入一個數:"); int x=sc.nextInt(); //通過對象獲取數據 if (x%2==0) { //判斷x是奇數還是偶數 System.out.println("輸入的是偶數"); }else { System.out.println("輸入的是奇數"); } } }
- 根據成績輸出對應的等級,使用if多分支和switch語句分別實現。
a) A級 [90,100]
b) B級 [80,89)
c) C級 [70,79)
d) D級 [60,69)
e) E級 [0,59)
if多分支語句
import java.util.Scanner;//導包 class Take2_1 { public static void main(String[] args) { Scanner sc=new Scanner(System.in);//創建鍵盤錄入對象 System.out.print("請輸入成績:"); int x=sc.nextInt(); //通過對象獲取數據 if (x>=90 && x<=100) { System.out.println("輸入成績的等級為:A級"); }else if (x>=80 && x<=89) { System.out.println("輸入成績的等級為:B級"); }else if (x>=70 && x<=79) { System.out.println("輸入成績的等級為:C級"); }else if (x>=60 && x<=69) { System.out.println("輸入成績的等級為:D級"); }else if (x>=0 && x<=59) { System.out.println("輸入成績的等級為:E級"); }else { System.out.println("輸入成績的有誤,請重新輸入!"); } } }
switch語句
import java.util.Scanner;//導包 class Take2_2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in);//創建鍵盤錄入對象 System.out.print("請輸入成績:"); int x=sc.nextInt(); //通過對象獲取數據 if (x<0 || x>100) { System.out.println("輸入成績的有誤,請重新輸入!"); }else { switch (x/10) { case 10: case 9: System.out.println("輸入成績的等級為:A級"); break; case 8: System.out.println("輸入成績的等級為:B級"); break; case 7: System.out.println("輸入成績的等級為:C級"); break; case 6: System.out.println("輸入成績的等級為:D級"); break; default: System.out.println("輸入成績的等級為:E級"); break; } } } }
- 根據月份,輸出對應的季節,並輸出至少兩個描述該季節的成語和活動。
import java.util.Scanner;//導包 class Take3 { public static void main(String[] args) { Scanner sc=new Scanner(System.in);//創建鍵盤錄入對象 System.out.println("請輸入月份:"); int month=sc.nextInt(); //通過對象獲取數據 switch (month) { case 1: case 2: case 3: System.out.println("春天-春暖花開 春意盎然-植樹 踏青"); break; case 4: case 5: case 6: System.out.println("夏天-夏日炎炎 夏雨雨人-游泳 吃雪糕"); break; case 7: case 8: case 9: System.out.println("秋天-秋高氣爽 秋風蕭瑟-旅游 看楓葉"); break; case 10: case 11: case 12: System.out.println("冬天-冬山如睡 冬溫夏清-滑雪 堆雪人"); break; default: System.out.println("輸入的月份有誤!"); break; } } }
- 從鍵盤輸入一個班5個學生的分數,求和並輸出。
//從鍵盤輸入一個班5個學生的分數,求和並輸出。 import java.util.Scanner;//導包 class Take4 { public static void main(String[] args) { Scanner sc=new Scanner(System.in);//創建鍵盤錄入對象 int sum=0; for (int i=1;i<=5 ;i++ ) { System.out.println("請輸入"+"第"+i+"的成績:"); int grade=sc.nextInt(); //通過對象獲取數據 if (grade<0 && grade<100) { System.out.println("輸入有誤,請重新輸入"); } sum=sum+grade; } System.out.println("一個班5個學生的分數的和是:"+sum); } }
六、 可選題
- 根據考試成績輸出對應的禮物,90分以上爸爸給買電腦,80分以上爸爸給買手機, 60分以上爸爸請吃一頓大餐,60分以下爸爸給買學習資料。
要求:該題使用多重if完成
import java.util.Scanner;//導包 class Take5 { public static void main(String[] args) { Scanner sc=new Scanner(System.in);//創建鍵盤錄入對象 System.out.print("請輸入成績:"); int x=sc.nextInt(); //通過對象獲取數據 if (x>=90) { System.out.println("爸爸給買電腦"); }else if (x>80 && x<=90) { System.out.println("爸爸給買手機"); }else if (x>60 && x<=80) { System.out.println("爸爸請吃一頓大餐"); }else if (x>0 && x<=60) { System.out.println("爸爸給買學習資料"); }else { System.out.println("輸入的成績的有誤!"); } } }
給20塊錢買可樂,每瓶可樂3塊錢,喝完之后退瓶子可以換回1塊錢,問最多可以喝到多少瓶可樂。