循环语句作业LJY

 

选择语句+循环语句作业

 

一、 填空题

  1. Java中有两种类型的选择结构的控制语句,分别是if语句和      switch        
  2. Java JDK1.7之前,switch只能支持byteshortcharint或者其对应的封装类以及Enum类型。在JDK1.7中又加入了     string       类型。
  3. for循环的语法格式是for (表达式1;表达式2;表达式3) {循环体},其中在整个循环过程中只执行一次的部分是     表达式1      
  4. 在循环结构中,如果想跳出循环体,结束整个循环结构可以使用     break    语句。
  5. ______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代码,编译运行的输出结果是(  A  )。(选择一项)

 

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代码,其执行结果是(   cd  )。(选择一项)case穿透

 

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;

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;}

 

B.

for (int x =0; x<=100;x+=2){ sum=sum+x;}

 

C.

for (int x =1; x<=100;x+=2){ sum=sum+x;}

 

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分)

  1. if语句的条件表达式的结果都必须是boolean值。( √  )
  2. switch选择语句是多分支选择语句,只能处理等值条件判断的情况,表达式可以是int类型、char类型,但不能是double,float类型。( √  )
  3. while循环结构的特点是先循环再判断,循环体至少执行一次。(  ×  
  4. for循环的语法格式是for (表达式1;表达式2;表达式3) {循环体},其中三个表达式都可以省略。(   √ )
  5. break语句可以出现在switch语句和循环语句中。( √    
  6. continue语句可以出现在switch语句和循环语句中。(   √  

 

四、 简答题

  1. if多分支语句和switch语句的异同之处

都是先判断,然后再看执行哪个.if 语句里时通过条件表达式来判断,一个个的往下判断

适用于范围.

Switch时判断case的值来决定执行哪条语句的,对于固定值效率更高

 

 

  1. whiledo-while语句的异同之处

  都是需要先写初始化语句,其变量不被释放可以继续使用,

while先判断后执行循环,do先执循环体,后进行判断,至少会执行一次循环体

  1. breakcontinue语句的作用

  break跳出循环,continue,终止本次循环继续下次循环

 

五、 编码题

  1. 输入一个数,判断是奇数还是偶数

 

import java.util.Scanner;

class Homework_first {

public static void main(String[] args) {

//输入一个数,判断是奇数还是偶数

 

 

Scanner sc = new Scanner(System.in);

System.out.println("请输入一个数");

int a = sc.nextInt();

 

if (a % 2 == 0) {

System.out.println(a + "是偶数");

}else

System.out.println(a + "是奇数");

}

}

 

   

  1. 根据成绩输出对应的等级,使用if多分支和switch语句分别实现。

a) A级   [90,100]

b) B级   [80,90)

c) C级   [70,80)

d) D级   [60,70)

e) E级   [0,60)

   import java.util.Scanner;

class Homework_Second {

public static void main(String[] args) {

/*2.根据成绩输出对应的等级,使用if多分支和switch语句分别实现。

a)A级   [90,100]

b)B级   [80,90)

c)C级   [70,80)

d)D级   [60,70)

e)E级   [0,60)*/

 

 

Scanner sc = new Scanner(System.in);

System.out.println("请输入成绩");

int a = sc.nextInt();

 

if (a >= 90 && a <= 100) {

System.out.println("A");

}else if (a >= 80 && a <=90) {

System.out.println("B");

}else if (a >= 70 && a < 80) {

System.out.println("C");

}else if (a >= 60 && a < 70) {

System.out.println("D");

}else if (a >= 0 && a < 60) {

System.out.println("E");

}

else System.out.println("输入成绩有误");

}

}

 

  1. 根据月份,输出对应的季节,并输出至少两个描述该季节的成语和活动

 

import java.util.Scanner;

class MonthA {

public static void main(String[] args) {

//根据月份,输出对应的季节,并输出至少两个描述该季节的成语和活动

Scanner sc = new Scanner(System.in);

System.out.println("请输入月份");

int m = sc.nextInt();

 

if (m <= 2 || m == 12) {

System.out.println("冬季-冰封万里-寒冬腊月");

}

else if (m > 2 && m <= 5) {

System.out.println("春季-春暖花开-春节");

}

else if (m > 5 && m <= 8) {

System.out.println("夏季-七月流火-吹空调");

}

else if (m > 8 && m <= 11) {

System.out.println("秋季-秋风萧瑟-国庆放假");

}

else {System.out.println("你输入的月份有误");}

/*class Test11 {

public static void main(String[] args) {

 

Scanner sc = new Scanner(System.in); //创建键盘录入对象

System.out.println("请输入月份");

int month = sc.nextInt(); //将键盘录入的结果存储在month

 

if (month > 12 || month < 1) {

System.out.println("对不起没有对应的季节");

}else if (month >= 3 && month <= 5) {

System.out.println(month + "月是春季");

}else if (month >= 6 && month <= 8) {

System.out.println(month + "月是夏季");

}else if (month >= 9 && month <= 11) {

System.out.println(month + "月是秋季");

}else {

System.out.println(month + "月是冬季");

}

}

}*/

 

 

 

 

 

 

 

 

 

}

}

 

 

 

  1. 从键盘输入一个班5个学生的分数,求和并输出。

import java.util.Scanner;

class  Add{

public static void main(String[] args) {

//4.从键盘输入一个班5个学生的分数,求和并输出。

Scanner sc = new Scanner(System.in);

System.out.println("请输入分数");

int a = sc.nextInt();

 

System.out.println("请输入分数");

int b = sc.nextInt();

 

System.out.println("请输入分数");

int c = sc.nextInt();

 

System.out.println("请输入分数");

int d = sc.nextInt();

 

System.out.println("请输入分数");

int e = sc.nextInt();

 

int sum = a + b + c + d + e;

System.out.println(sum);

 

 

 

}

}

 

 

 

六、 可选题

  1. 根据考试成绩输出对应的礼物,90分以上爸爸给买电脑,80分以上爸爸给买手机, 60分以上爸爸请吃一顿大餐,60分以下爸爸给买学习资料。

要求:该题使用多重if完成

不知道满分多少...

import java.util.Scanner;

class Alter {

public static void main(String[] args) {

/*1.根据考试成绩输出对应的礼物,90分以上爸爸给买电脑,80分以上爸爸给买手机,

60分以上爸爸请吃一顿大餐,60分以下爸爸给买学习资料。

要求:该题使用多重if完成*/

Scanner sc = new Scanner(System.in);

int score = sc.nextInt();

if (score > 90) {

System.out.println("电脑");

}else if (score > 80 && score < 90) {

System.out.println("手机");

}else if (score > 70 && score < 80) {

System.out.println("吃大餐");

}else if (score >= 0 && score < 60) {

System.out.println("买学习资料");

}else System.out.println("你输入的成绩有错误");

}

}

 

 

20块钱买可乐,每瓶可乐3块钱,喝完之后退瓶子可以换回1块钱,问最多可以喝到多少瓶可乐。

class Cola {

public static void main(String[] args) {

//20块钱买可乐,每瓶可乐3块钱,喝完之后退瓶子可以换回1块钱,问最多可以喝到多少瓶可乐。

 

int count = 0; //喝的瓶数

for ( int sum = 20; sum >= 3 ;sum++ ) { //把总钱数定义了,每次少三块钱

sum -= 3; count++;

}

System.out.print(count);

 

}

}

感觉用加减法效率慢用除法做了一次感觉好像更麻烦了.

class Cola {

public static void main(String[] args) {

//20块钱买可乐,每瓶可乐3块钱,喝完之后退瓶子可以换回1块钱,问最多可以喝到多少瓶可乐。

 

int count = 0; //喝的瓶数

for ( int sum = 20; sum >= 3 ; ) { //把总钱数定义了

count = sum / 3 + count ; //每次都把所有钱买可乐

sum = sum / 3 + sum % 3; //换完瓶的总钱数

 

}

System.out.print(count);

 

}

}

 

 

posted on 2018-07-06 19:35  黑的发白  阅读(396)  评论(0编辑  收藏  举报

导航