栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

面向对象程序设计(Java)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

面向对象程序设计(Java)

7-1 认识静态方法
  • 第1关:选择题

    1.设静态方法sum定义如下,则其中void的含义是()。
    static void sum(){ }

    A、该方法没有返回值
    B、方法执行后不再返回
    C、方法返回值为任意类型
    D、以上三个都不对
  • A
    

    2、设程序如下,下面的说法错误的是()

    • public class Test {

      1. public static void main(String[] args) {
      2. System.out.printf("%.3fn" ,fun(2, 3));
      3. }
      4. public static double fun(int a,int b) {
      5. return (double)a/b;
      6. }

      }

      A、类Test中包含两个方法,main()、 fun()
      B、方法fun()的返回值是double类型的
      C、方法fun()中有两个形参,都是int类型的
      D、语句System.out.printf("%.3fn" ,fun(2, 3));中的参数2和3是形参
    • 以下程序的运行结果是()。
      public class Test {

    • public static void main(String[] args) {
    • int a=2,b=4,c;
    • c=fun(a,b);
    • System.out.printf("%dn",c);
    • }
    • static int fun(int x, int y)
    • {
    • int z;
    • z=(x
    • return z;
    • }
    • }

      A、2

      B、4

      C、不确定

    • A
      第2关:求和:1+3+5+...
    • 本关任务:2. 编写函数fun(),求1+3+5+7+……+n. 在main()方法中输入n的值(要求为奇数),调用fun()求和,在main()方法中并输出结果。 
    • import java.util.Scanner;
      public class SumOdd {
      
      	public static void main(String[] args) {
      		
      	 int n;  //表示输入的奇数
          System.out.println("输入一个整数(奇数)");
          Scanner sc = new Scanner(System.in);
      
          //调用fun()求和,并输出结果
          int m = sc.nextInt();
          System.out.println(fun(m));
      }
              public static int fun(int m) {
                  int sum=0;
                  //计算1+3+5+7+……+m
                  for(int i=0;i 7-2 静态方法的定义和调用 
    • 第1关:选择题

      1.以下程序的运行结果是()
      public class Test{
      public static void main(String args[]) {
      int a=10,b=20;
      swap(a,b);
      System.out.printf("a=%d,b=%d",a,b);
      }

       
          
      1. public static void swap(int d1,int d2) {
      2. int temp;
      3. temp = d1;
      4. d1 = d2;
      5. d2 = temp;
      6. }

      }

      A、a=10,b=20
      B、a=20,b=10
      C、不确定
    • A

    • 2、

      以下程序的运行结果是()
      public class Test{
      public static void main(String args[]) {
      int a=10,b=20;
      swap(a,b);
      System.out.printf("a=%d,b=%d",a,b);
      }

       
          
      1. public static void swap(int d1,int d2) {
      2. int temp;
      3. temp = d1;
      4. d1 = d2;
      5. d2 = temp;
      6. System.out.printf("d1=%d,d2=%d",d1,d2);
      7. }

      }

      A、

      d1=10,d2=20
      a=10,b=20

      B、

      d1=20,d2=10
      a=20,b=10

      C、

      d1=20,d2=10
      a=10,b=20

      D、

      不确定

    • C

      以下程序的运行结果是()
      public class Test{
      public static void main(String args[]) {

       
    • int data[] = { 12, 4, 6, 8, 10 };
    • int min;
    • min = getData(data);
    • System.out.println(min);
    • }
    • public static int getData(int[] arr){
    • int d=0;
    • for(int i=1 ; i
    • if(arr[d]>arr[i]){
    • }

      A、12

      B、4

      C、6

    • D、10

    • B
      第2关:判素数
    • import java.util.Scanner;
      public class IsPrime {
          public static void main(String[] args) {
              System.out.println("输入一个大于2的自然数n");
              Scanner sc = new Scanner(System.in);
              int n = sc.nextInt();
              //调用prime(),并根据其返回值判断n是否为素数
              if (prime(n)) {
                  System.out.println(n+"是素数");
              } else {
                  System.out.println(n+"不是素数");
              }
          }
          //判素数,是则返回true,不是则返回false
          public static boolean prime(int m) {
              for(int i=2;i 

      第3关:数组排序
    • 测试输入:15 10 8 12 20 30 2 4 7 9;
      预期输出:
      30,20,15,12,10,9,8,7,4,2,
    • import java.util.Scanner;
      public class Sort2 {
      public static void main(String[] args) {  
          int[] data = new int[10];  
          Scanner sc = new Scanner(System.in);  
          //输入10个整数  
          for(int k=0;k 7-3 递归方法 
    • 第1关:猴子摘桃(递归问题)
    • 本关任务: 猴子吃桃问题:小猴子摘了一堆桃子。第一天吃掉一半又多吃了1个,第二天吃了剩下的一半又多吃1个,以后每天都吃掉剩下的一半多一个。第10天发现只剩一个桃子了。问第一天摘了多少桃子?第二天还有多少桃子?第三天……
      编写方法peach(int day),计算第day天的桃子数。
      在main()方法中输入day(1~10),即你想知道第day天小猴子有多少桃子,调用peach()方法求该天的桃子数。
    • 
      import java.util.Scanner;
      public class Peach {
              public static void main(String[] args) {
                  Scanner sc = new Scanner(System.in);
                  System.out.println("你想知道小猴子第几天的桃子数?请输入(1~10)");
                  int day = sc.nextInt();
                  System.out.println("第"+day+"天的桃子数是"+peach(day));
              }
              public static int peach(int x){
                  if(x==10){
                      return 1;
                  } else {
                      return 2* peach(x+1) + 2;
                  }
              }
      }
      第2关:用递归将整数逆序输出
    • 为了完成本关任务,你需要掌握:
      1.求整数n的个位、个位之前的数字组成的整数,2.递归问题分析。
      比如, 求5!.
    • 
      import java.util.Scanner;
      public class Reverse {
          public static void main(String[] args) {
              int n;
              Scanner sc = new Scanner(System.in);
              n = sc.nextInt();
      		//调用reverse(),将整数n的各位数字逆序输出
              reverse(n);
              sc.close();
          }
              //逆序输出各位数字
          static void reverse(int n)
          {
              if(n > 0)
              {
                  System.out.printf("%4d",n%10);
                  reverse(n/10);
              }
          }
      }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/876493.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号