方法定义及调用 练习

方法定义及调用 练习

标签 :Java入坑之旅


0x00. 题目

  1. 编写一个方法,求整数n的阶乘,例如5的阶乘是12345。 [必做题]
  2. 编写一个方法,判断该年份是平年还是闰年。[必做题]
  3. 编写一个方法,输出大于200的最小的质数。[选做题]
  4. 编写一个方法,功能:定义一个一维的int 数组,长度任意,然后将它们按从小到大的顺序输出(使用冒泡排序)(知识点:方法的定义和访问)。[选做题]

0x01. 题目01

  1. 编写一个方法,求整数n的阶乘,例如5的阶乘是12345。 [必做题]
// FunctionFactorial.java
package com.ryanjie.test;

import java.util.Scanner;

/**   
 * @ClassName:FunctionFactorial   
 * @Description: 编写一个方法,求整数n的阶乘,例如5的阶乘是1*2*3*4*5。
 * @author: Ryanjie
 * @date:2018年8月10日 下午4:18:22   
 *     
 * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
 */
public class FunctionFactorial {

    /**   
     * @Title: factorial   
     * @Description: 求整数n的阶乘
     * 
     * @param num      
     * @return: void      
     * @throws   
     */
    public static void factorial(int num) {
        long sum = 1;
        for (int i = 1; i <= num; i++) {
            sum *= i; 
        }
        
        System.out.println(num + " 的阶乘为: " + sum);
    }
    
    public static void main(String[] args) {
        System.out.println("Please input a number<int> :");
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();
        factorial(number);
        in.close();
    }

}

0x02. 题目02

  1. 编写一个方法,判断该年份是平年还是闰年。[必做题]
//FunctionLeapYear.java
package com.ryanjie.test;

import java.util.Scanner;

/**   
 * @ClassName:FunctionLeapYear   
 * @Description:编写一个方法,判断该年份是平年还是闰年。
 * @author: Ryanjie
 * @date:2018年8月10日 下午4:41:28   
 *     
 * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
 */
public class FunctionLeapYear {

    /**   
     * @Title: leapYear   
     * @Description: 判断该年份是平年还是闰年。
     * 
     * @param year      
     * @return: void      
     * @throws   
     */
    public static void isLeapYear(long year) {
        boolean flag = false;
        if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
            flag = true;
        }
        
        if(flag) {
            System.out.println(year + " 是闰年!");
        }
        else {
            System.out.println(year + " 是平年!");
        }
    }
    
    public static void main(String[] args) {
        System.out.println("Please input a year<int>: ");
        Scanner in = new Scanner(System.in);
        long year = in.nextLong();
        isLeapYear(year);
        in.close();
    }

}

0x03. 题目03

  1. 编写一个方法,输出大于200的最小的质数。[选做题]
//FunctionPrimeNumber.java
package com.ryanjie.test;

import java.util.Scanner;

/**   
 * @ClassName:FunctionPrimeNumber   
 * @Description: 编写一个方法,输出大于200的最小的质数。
 * @author: Ryanjie
 * @date:2018年8月10日 下午4:51:24   
 *     
 * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
 */
public class FunctionPrimeNumber {

    /**   
     * @Title: isPrime   
     * @Description: 判断这个数是否为质数
     * 
     * @param num
     * @return      
     * @return: boolean      
     * @throws   
     */
    public static boolean isPrime(int num) {
        
        if(num > 2 && (num & 1) ==0) {
            return false;
        }
        
        for(int i = 3; i * i <= num; i += 2) { 
            if(num % i ==0 ) {
                return false;
            }
        }
        return true;
    }
    
    /**   
     * @Title: findNumber   
     * @Description: 输出大于200的最小的质数
     * 
     * @param number      
     * @return: void      
     * @throws   
     */
    public static void findNumber(int number) {
        System.out.print("大于 " + number + " 的最小的质数为:");
        while (!isPrime(number)) {
            number ++;
        }
        System.out.println(number);
    } 
    
    public static void main(String[] args) {
        System.out.println("Please input a number<long>:");
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        findNumber(num);
        in.close();
    }

}

0x04. 题目04

  1. 编写一个方法,功能:定义一个一维的int 数组,长度任意,然后将它们按从小到大的顺序输出(使用冒泡排序)(知识点:方法的定义和访问)。[选做题]
//FunctionBubbleSort.java
package com.ryanjie.test;

import java.util.Random;

/**   
 * @ClassName:FunctionBubbleSort   
 * @Description:定义一个一维的int 数组,长度任意,然后将它们按从小到大的顺序输出(使用冒泡排序)
 * @author: Ryanjie
 * @date:2018年8月10日 下午7:16:50   
 *     
 * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
 */
public class FunctionBubbleSort {
    
    /**   
     * @Title: printAll   
     * @Description: 打印整个数组
     * 
     * @param arr      
     * @return: void      
     * @throws   
     */
    public void printAll(long[] arr) {
        int flag = 0;
        for(long value : arr) {
            System.out.print(value + "\t");
            if ((++ flag) % 5 == 0) {
                System.out.println();
            }
        }
    }

    /**   
     * @Title: bubbleSort   
     * @Description: 自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,
     *              较小的往上冒
     *       
     * @return: void      
     * @throws   
     */
    public void bubbleSort(long[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    long temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1]= temp; 
                }
            }
        }
        System.out.println("冒泡排序算法排序后的数组为:");
        this.printAll(arr);
    }
    
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        FunctionBubbleSort FB = new FunctionBubbleSort();
        long[] arr = new long[100];
        Random r = new Random();
        for(int i = 0; i < 100; i++) {
            arr[i] = r.nextInt(); 
        }
        System.out.println("排序前的数组为:");
        FB.printAll(arr);
        FB.bubbleSort(arr);
    }

}

猜你喜欢

转载自www.cnblogs.com/Ryanjie/p/9457114.html
今日推荐