zoukankan      html  css  js  c++  java
  • 7.7 作业 韩宗轩

     

    选择语句+循环语句作业

    一、    填空题

    1. Java中有两种类型的选择结构的控制语句,分别是if语句和     swich        
    2. 在Java JDK1.7之前,switch只能支持byte、short、char、int或者其对应的封装类以及Enum类型。在JDK1.7中又加入了       boolean     类型。
    3. for循环的语法格式是for (表达式1;表达式2;表达式3) {循环体},其中在整个循环过程中只执行一次的部分是    表达式1      
    4. 在循环结构中,如果想跳出循环体,结束整个循环结构可以使用      break    语句。
    5. ________continue_____语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。即只结束本次循环,而不是终止整个循环的执行。

     

     

    二、    选择题

     

    1.

    以下代码的执行结果是(  C  。(选择一项)

     

    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的定义,(  DC  )可使以下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;

                                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循环代码的执行结果是(  B  。(选择一项)

     

    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中有如下代码,则编译运行该类的输出结果是(   B )。(选择一项)

     

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

     

    四、    简答题

    1. if多分支语句和switch语句的异同之处
    2. while和do-while语句的异同之处
    3. break和continue语句的作用

     

    五、    编码题

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

    import java.util.Scanner;

    class Wu1 {

             public static void main(String[] args) {

                       Scanner Sc= new Scanner(System.in);

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

                       int i = Sc.nextInt();

                       if (i%2==0) {

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

                       }else {System.out.println("奇数");

                       }

             }

    }

    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 Wu2 {

             public static void main(String[] args) {

                       Scanner sc= new Scanner(System.in);

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

                       int i = sc.nextInt();

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

                                System.out.println("A级");

                       }

                       else if (i>=80&&i<90) {

                                System.out.println("B级");

                       }

                       else if (i>=70&&i<80) {

                                System.out.println("C级");

                       }

                       else if (i>=60&&i<70) {

                                System.out.println("D级");

                       }

                       else if (i>=0&&i<60) {

                                System.out.println("E级");

                       }

                         else  {

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

                       }

     

             }

    }

     

     

     

     

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

     

     

    import java.util.Scanner;

    class Wu3 {

             public static void main(String[] args) {

                       Scanner sc= new Scanner(System.in);

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

                       int i = sc.nextInt();

                       if (i>=2&&i<=4) {

                                System.out.println("春天-春暖花开"+' '+"春意盎然-植树"+' '+"踏青");

                       }else if (i>=5&&i<=7) {

                                System.out.println("夏天-春暖花开"+' '+"春意盎然-植树"+' '+"踏青");

                       }else if (i>=8&&i<=10) {

                                System.out.println("秋天-春暖花开"+' '+"春意盎然-植树"+' '+"踏青");

                       }else {

                                System.out.println("冬天-春暖花开"+' '+"春意盎然-植树"+' '+"踏青");

                       }

             }

    }

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

     

    import java.util.Scanner;

    class Wu4 {

             public static void main(String[] args) {

                       Scanner sc1=new Scanner(System.in);

                       System.out.println("请输入第一个学生的成绩");

                       int a =sc1.nextInt();

                       Scanner sc2=new Scanner(System.in);

                       System.out.println("请输入第二个学生的成绩");

                       int b =sc2.nextInt();

                       Scanner sc3=new Scanner(System.in);

                       System.out.println("请输入第三个学生的成绩");

                       int c =sc3.nextInt();

                       Scanner sc4=new Scanner(System.in);

                       System.out.println("请输入第四个学生的成绩");

                       int d =sc4.nextInt();

                       Scanner sc5=new Scanner(System.in);

                       System.out.println("请输入第五个学生的成绩");

                       int e =sc5.nextInt();

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

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

             }

    }

     

    import java.util.Scanner;

     

    class Wu44 {

     

             //static int a = 1;

     

             public static void main(String[] args) {

                       Scanner sc=new Scanner(System.in);

                       int n = 0;

                       int sum = 0;

                       while ( n < 5) {

                                n++;

                                System.out.println("请输入第"+n+"个学生的成绩");

                                int q =sc.nextInt();

                                sum += q;

                               

                       }

            

                       System.out.println(sum);

             }

    六、    可选题

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

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

     

     

    import java.util.Scanner;

    class Xuan1 {

             public static void main(String[] args) {

                       Scanner sc=new Scanner(System.in);

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

                        int i= sc.nextInt();

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

                                System.out.println("爸爸给买电脑");

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

                                System.out.println("爸爸给买手机");

                       }else if (i>60&&i<=80) {

                                System.out.println("爸爸请吃一顿大餐");

                       }else

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

             }

    }

     

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

     

     

    class Xuan2 {

             public static void main(String[] args) {

                       int count = 0;

                       for (int m=20;m/3>=1 ;m++ ) {

                                          m=m-3;

                                          count++;

                                 }

                                 System.out.println(count);

             /*int m =20;

             int count=0;

             while (m/3>=1) {

                       m=m-3;

                       m++;

                       count++;

             }

             System.out.println(count);

             int m =20;

             int count =0;

             do {

                       m=m-3;

                       m++;

                       count++;

                      

             }

             while (m/3>=1);

             System.out.println(count);*/

             }

            

            

    }

     

  • 相关阅读:
    ASP.NET编程的十大技巧
    C#学习心得(转)
    POJ 1177 Picture (线段树)
    POJ 3067 Japan (树状数组)
    POJ 2828 Buy Tickets (线段树)
    POJ 1195 Mobile phones (二维树状数组)
    HDU 4235 Flowers (线段树)
    POJ 2886 Who Gets the Most Candies? (线段树)
    POJ 2418 Cows (树状数组)
    HDU 4339 Query (线段树)
  • 原文地址:https://www.cnblogs.com/Hveritas/p/9275375.html
Copyright © 2011-2022 走看看