第三章 控制语句

返回目录

 

选择题

    1. (单选)分析如下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");
        }
      }
      1. !(a&&b)
      2. !(a||b)
      3. ab
      4. !(a||b)ab
    2. (单选)给定如下Java代码,编译运行的结果是(A
      public class Test { 
        public static void main(String[] args) {
          int sum=0;
          for(int i=1;i<10;i++){
            do{
              i++;
              if(i%2!=0)
                sum+=i;
            }while(i<6);
          }
          System.out.println(sum);
        }
      }
      1. 8
      2. 15
      3. 24
      4. 什么也不输出
    3. (单选)以下关于方法调用的代码的执行结果是(B
      public class Test {
        public static void main(String args[]) {
          int i = 99;
          mb_operate(i);
          System.out.print(i + 100);
        }
        static void mb_operate(int i) {
          i += 100;
        }
      }
      1. 99
      2. 199
      3. 299
      4. 99100
    4. (选2项)下列选项中关于变量x的定义,(B,D)可使以下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");  
      }
      1. double x = 100;
      2. char x = 100;
      3. String x = "100";
      4. int x = 100;
    5. (选2项)以下选项中添加到代码中横线处会出现错误的是(B,D
      public class Test {
        public float aMethod(float a, float b) {
          return 0; 
        }
      }
      1. public float  aMethod(float a, float b, float c) {return 0;}
      2. public float  aMethod(float c, float d) {return 0;}
      3. public int  aMethod(int a, int b) {return 0;}
      4. private int  aMethod(float a, float b) {return 0;}

 

编码题

    1. 用while循环分别计算100以内的奇数及偶数的和,并输出。
      点击查看答案
      public class test {
        public static void main(String[] args) {
          int a = 1;
          int sum = 0; //储存奇数的和
          int sum2 = 0; //储存偶数的和
          while(a<=100) {
            if(a%2==0) { //判断a%2==0,余0就是偶数
              sum2 += a; //符合条件sum2 = sum2+a
            }else{ //否则,就是奇数
              sum += a; //符合条件sum = sum+a
            }
            a++; //a自增,计数
          }
          System.out.println("奇数和:" + sum);
          System.out.println("偶数和:" + sum2);
        }
      }
    2. 用for循环输出1-1000之间能被5整除的数,且每行输出5个。
      点击查看答案
      public class test {
        public static void main(String[] args) {
          for(int a=1, b=0; a<=1000; a++) {
            if(a%5==0) { //判断a%5==0,余0就是能被5整除的数
              System.out.print(a + "\t");
              b++; //b用于计次数,自增
            }
            if(b==5) { //判断b是否达到5次,达到执行if内语句
              System.out.println(); //输出空行,达到“每行输出5个”目标
              b = 0; //b归零,重新计数
            }
          }
        }
      }
    3. 从键盘输入某个十进制整数数,转换成对应的二进制整数并输出
      点击查看答案
      import java.util.Scanner;
      public class test {
        public static void main(String args[]){
          Scanner sc = new Scanner(System.in);
          System.out.println("请输入一个十进制整数");
          int ten = sc.nextInt(); //输入的十进制整数
          for(int a=10; a >= 0; a--) { //决定位数
            System.out.print(ten >>> a&1); //利用位移
          }
        }
      }
    4. 编程求:∑1+∑2+……+∑100
      点击查看答案
      public class test {
        public static void main(String args[]){
          int sum = 0;
          for(int a=1; a<=100; a++) {
            for(int b=1; b<=a; b++) {
              sum += b;
            }
          }
          System.out.println(sum);
        }
      }
    5. 编写递归算法程序:一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求数列的第40位数
      点击查看答案
      import java.util.Scanner;
      public class test {
        public static void main(String[] args) { 
          Scanner input = new Scanner(System.in); 
          System.out.print("请输入需要查找的第几位数:"); 
          int n = input.nextInt(); 
          System.out.println("第" + n + "数是:" + findRegular(n)); 
          }
        static int findRegular(int n) {
            if (n <= 2) {//前两个数为1
                return 1;// 递归头
            } else {//f(n)=f(n-1)+f(n-2)
                n = findRegular(n - 1) + findRegular(n - 2);// 递归体
            }
            return n;
        }
      }