方法定義及調用 練習

方法定義及調用 練習

標籤 :Java入坑之旅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);
    }

}
相關文章
相關標籤/搜索