流程控制語句練習

流程控制語句練習

標籤 :Java入坑之旅java


0x00. 題目

  1. 企業發放的獎金根據利潤提成。利潤低於或等於10萬元時,獎金可提10%;利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可提成7.5%;20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於40萬元的部分,可提成3%;60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於100萬元時,超過100萬元的部分按1%提成,在程序中設定一個變量爲當月利潤,求應發放獎金總數?(知識點:條件語句) [必作題]
  2. 給定一個成績a,使用switch結構求出a的等級。A:90-100,B:80-89,C:70-79,D:60-69,E:0~59(知識點:條件語句switch)[必作題]
  3. 假設某員工今年的年薪是30000元,年薪的年增加率6%。編寫一個Java應用程序計算該員工10年後的年薪,並統計將來10年(從今年算起)總收入。(知識點:循環語句for)[選作題]
  4. 猴子第一天摘下若干個桃子,立即吃了一半,還不癮,又多吃了一個,次日早上又將剩下的桃子吃掉一半,又多吃了一個。之後天天早上都吃了前一天剩下的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。(知識點:循環語句 while)[選作題]
  5. 輸入一個數字,判斷是一個奇數仍是偶數(知識點:條件語句) [必作題]
  6. 編寫程序, 判斷一個變量x的值,若是是1,輸出x=1,若是是5,輸出x=5,若是是10,輸出x=10,除了以上幾個值,都輸出x=none。(知識點:條件語句) [必作題]
  7. 判斷一個數字是否能被5和6同時整除(打印能被5和6整除),或只能被5整除(打印能被5整除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)(知識點:條件語句) [必作題]
  8. 輸入一個年份,判斷這個年份是不是閏年(知識點:條件、循環語句) [必作題]
  9. 輸入一個0~100的分數,若是不是0~100之間,打印分數無效,根據分數等級打印A,B,C,D,E(知識點:條件語句if elseif) [必作題]
  10. 輸入三個整數x,y,z,請把這三個數由小到大輸出(知識點:條件語句) [必作題]
  11. 有一個很少於5位的正整數,求它是幾位數,分別打印出每一位數字。(知識點:條件語句) [必作題]
  12. 編寫一個程序,計算郵局匯款的匯費。若是匯款金額小於100元,匯費爲一元,若是金額在100元與5000元之間,按1%收取匯費,若是金額大於5000元,匯費爲50元。匯款金額由命令行輸入。(知識點:條件語句) [選作題]
  13. 分別使用for循環,while循環,do循環求1到100之間全部能被3整除的整數的和。(知識點:循環語句) [選作題]
  14. 輸出0-9之間的數,可是不包括5。 [選作題]
  15. 編寫一個程序,求整數n的階乘,例如5的階乘是12345 [選作題]
  16. 編寫一個程序,找出大於200的最小的質數[選作題]
  17. 由命令行輸入一個4位整數,求將該數反轉之後的數,如原數爲1234,反轉後的數位4321 [選作題]

0x01. 題01-06

  1. 企業發放的獎金根據利潤提成。利潤低於或等於10萬元時,獎金可提10%;利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可提成7.5%;20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於40萬元的部分,可提成3%;60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於100萬元時,超過100萬元的部分按1%提成,在程序中設定一個變量爲當月利潤,求應發放獎金總數?(知識點:條件語句) [必作題]git

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:TotalPrizes   
     * @Description: total prizes 
     * @author: Ryanjie
     * @date:2018年8月8日 下午10:31:27   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class TotalPrizes {
    
        /**   
         * @Title: total   
         * @Description: the total of prizes of profit
         * 
         * @param profit      
         * @return: void      
         * @throws   
         */
        public static void total(double profit) {
            if (profit <= 10) {
                System.out.print(0.1 * profit);
            }
            else if (profit <= 20) {
                System.out.print(0.1 * 10 + 0.075 * (profit - 10) );
            }
            else if (profit <= 40) {
                System.out.print(0.1 * 10 + 0.075 * 10 + 0.05 * (profit - 20));
            }
            else if (profit <= 60) {
                System.out.print(0.1 * 10 + 0.075 * 10 + 0.05 * 20 + 0.03 * (profit - 40));
            }
            else if (profit <= 100) {
                System.out.print(0.1 * 10 + 0.075 * 10 + 0.05 * 20 + 0.03 * 20 + 0.015 * (profit - 60));
            }
            else {
                System.out.print(0.1 * 10 + 0.075 * 10 + 0.05 * 20 + 0.03 * 20 + 0.015 * 40 + 0.01 * (profit - 100));
            }
    
        }
        public static void main(String[] args) {
            while (true) {
                System.out.println("Please input the profit<int number> :");
                Scanner in = new Scanner(System.in);
                double profit  = in.nextInt();
                System.out.print("the total prizes is ");
                total(profit);
                System.out.println("(萬元)");
            }
        }
    
    }
  2. 給定一個成績a,使用switch結構求出a的等級。A:90-100,B:80-89,C:70-79,D:60-69,E:0~59(知識點:條件語句switch)[必作題]ide

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @Title:  ScoresGrade.java   
     * @Package com.ryanjie.test   
     * @ClassName:ScoresGrade   
     * @Description:Scores Grade
     * @author: Ryanjie
     * @date:2018年8月8日 下午4:21:02   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class ScoresGrade {
    
        /**   
         * @Title: grade   
         * @Description: 判斷成績等級
         * 
         * @param score      
         * @return: void      
         * @throws   
         */
        public static void grade(double score) {
            if (score < 60) {
                System.out.println("garde: E");
            }
            else if (score < 70) {
                System.out.println("grade: D");
            }
            else if (score < 80) {
                System.out.println("grade: C");
            }
            else if (score < 90) {
                System.out.println("grade:B");
            }
            else if (score <=100) {
                System.out.println("grade:A");
            }
            else {
                System.out.println("您輸入的成績有錯!");
            }
    
        }
    
        /**   
         * @Description: ScoreGrade
         */
        public static void main(String[] args) {
            // TODO 自動生成的方法存根
    
            System.out.println("Please input your score <0~100>:");
            Scanner in = new Scanner(System.in);
            double score = in.nextInt();
            grade(score);
        }
    
    }
  3. 假設某員工今年的年薪是30000元,年薪的年增加率6%。編寫一個Java應用程序計算該員工10年後的年薪,並統計將來10年(從今年算起)總收入。(知識點:循環語句for)[選作題]命令行

    package com.ryanjie.test;
    
    /**   
     * @ClassName:TotalRevenue   
     * @Description:the total of income 
     * @author: Ryanjie
     * @date:2018年8月8日 下午11:43:10   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class TotalRevenue {
    
        /**   
         * @Title: total   
         * @Description: the wages and total revenue after 10 years
         * 
         * @param sal
         * @param rate
         * @param years      
         * @return: void      
         * @throws   
         */
        public static void total(double sal, double rate, int years) {
            double sum = 0;
            for(int i = 0; i <= years; i++) {
                sum += sal;
                sal += (rate * sal);
            }
            System.out.println("wages after " + years + "years: " + sal + "(元)");
            System.out.println("total revenue after " + years + "years: " + sum + "(元)");
        }
        public static void main(String[] args) {
            System.out.println();
            double sal = 30000;
            double rate = 0.06;
            int years = 10;
            total(sal, rate, years);
        }
    
    }
  4. 猴子第一天摘下若干個桃子,立即吃了一半,還不癮,又多吃了一個,次日早上又將剩下的桃子吃掉一半,又多吃了一個。之後天天早上都吃了前一天剩下的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。(知識點:循環語句 while)[選作題]code

    package com.ryanjie.test;
    
    /**   
     * @ClassName:MonkeyPickPeach   
     * @Description:monkey picking peach
     * @author: Ryanjie
     * @date:2018年8月8日 下午11:58:45   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class MonkeyPickPeach {
    
        /**   
         * @Title: sumPeach   
         * @Description: the sum of peach before 10 days
         * 
         * @param days      
         * @return: void      
         * @throws   
         */
        public static void sumPeach(int days) {
            int sum = 1;
            for(int i = 1; i < days; ++i) {
                sum = (++ sum) * 2;
            }
            System.out.println("Before " + days + " days, the sum of peach is " + sum);
        }
    
        public static void main(String[] args) {
            int days = 10;
            sumPeach(days);
        }
    
    }
  5. 輸入一個數字,判斷是一個奇數仍是偶數(知識點:條件語句) [必作題]three

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:JudgeOddEvenNumber   
     * @Description: Judge a number is odd number or even number
     * @author: Ryanjie
     * @date:2018年8月8日 下午5:14:01   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class JudgeOddEvenNumber {
    
        /**   
         * @Title: judge   
         * @Description: Judge number is odd number or even number
         * 
         * @param num      
         * @return: void      
         * @throws   
         */
        public static void judge(int num) {
            if(num % 2 == 0) {
                System.out.println("even number");
            }
            else {
                System.out.println("odd number");
            }
        }
    
        public static void main(String[] args) {
            // TODO 自動生成的方法存根
            System.out.println("Please input a number:");
            Scanner in = new Scanner(System.in);
            int number = in.nextInt();
            judge(number);
        }
    
    }
  6. 編寫程序, 判斷一個變量x的值,若是是1,輸出x=1,若是是5,輸出x=5,若是是10,輸出x=10,除了以上幾個值,都輸出x=none。(知識點:條件語句) [必作題]ip

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:JudgeValue   
     * @Description:Judge Numer Value 
     * @author: Ryanjie
     * @date:2018年8月8日 下午5:24:01   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class JudgeValue {
    
        /**   
         * @Title: judge   
         * @Description: Judge Numer Value
         * 
         * @param num      
         * @return: void      
         * @throws   
         */
        public static void judge(int num) {
            switch (num) {
            case 1: 
                System.out.println("1");
                break;
            case 5:
                System.out.println("5");
                break;
            case 10:
                System.out.println("10");
                break;
            default:
                System.out.println("none");
            }
        }
    
        public static void main(String[] args) {
    
            System.out.println("Please input a number:");
            Scanner in = new Scanner(System.in);
            int number = in.nextInt();
            judge(number);
    
        }
    
    }

0x02. 題07-12

  1. 判斷一個數字是否能被5和6同時整除(打印能被5和6整除),或只能被5整除(打印能被5整除),或只能被6整除,(打印能被6整除),不能被5或6整除,(打印不能被5或6整除)(知識點:條件語句) [必作題]rem

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:Divisible   
     * @Description:Divisible of n
     * @author: Ryanjie
     * @date:2018年8月8日 下午10:11:40   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class Divisible {
    
        /**   
         * @Title: divisibleByN   
         * @Description: divisible by n
         * 
         * @param num1
         * @param num2
         * @return      
         * @return: boolean      
         * @throws   
         */
        public static boolean divisibleByN(int num1, int num2) {
            boolean temp = false;
            if (num1 % num2 == 0) {
                temp = true;
            }
            return temp;
        }
    
        public static void main(String[] args) {
            while (true) {
                System.out.println("Please input a number <int>:");
                Scanner in = new Scanner(System.in);
                int number = in.nextInt();
    
                if(divisibleByN(number, 5) && divisibleByN(number, 6)) {
                    System.out.println(number + " divisible by 5 and 6");
                }
                else if (divisibleByN(number, 5)) {
                    System.out.println(number + " divisible by 5");
                }
                else if (divisibleByN(number, 6)) {
                    System.out.println(number + " divisible by 6");
                } 
                else {
                    System.out.println(number + " cannot divisible by 5 or 6");
                }
    
            }   
        }
    
    }
  2. 輸入一個年份,判斷這個年份是不是閏年(知識點:條件、循環語句) [必作題]get

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:LeapYear   
     * @Description: Judge year is leap year
     * @author: Ryanjie
     * @date:2018年8月8日 下午5:34:06   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class LeapYear {
    
        /**   
         * @Title: isLeapYear   
         * @Description: Judge Year Is LeepYear
         * 
         * @param year      
         * @return: void      
         * @throws   
         */
        public static void isLeapYear(int year) {
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
                System.out.println("Is LeepYear");
            }
            else {
                System.out.println("Is not Leepyear");
            }
        }
        public static void main(String[] args) {
    
            System.out.println("Please input year:");
            Scanner in = new Scanner(System.in);
            int year = in.nextInt();
            isLeapYear(year);
    
        }
    
    }
  3. 輸入一個0~100的分數,若是不是0~100之間,打印分數無效,根據分數等級打印A,B,C,D,E(知識點:條件語句if elseif) [必作題]input

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @Title:  ScoresGrade.java   
     * @Package com.ryanjie.test   
     * @ClassName:ScoresGrade   
     * @Description:Scores Grade
     * @author: Ryanjie
     * @date:2018年8月8日 下午4:21:02   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class ScoresGrade {
    
        /**   
         * @Title: grade   
         * @Description: 判斷成績等級
         * 
         * @param score      
         * @return: void      
         * @throws   
         */
        public static void grade(double score) {
            if (score < 60) {
                System.out.println("garde: E");
            }
            else if (score < 70) {
                System.out.println("grade: D");
            }
            else if (score < 80) {
                System.out.println("grade: C");
            }
            else if (score < 90) {
                System.out.println("grade:B");
            }
            else if (score <=100) {
                System.out.println("grade:A");
            }
            else {
                System.out.println("您輸入的成績有錯!");
            }
    
        }
    
        /**   
         * @Description: ScoreGrade
         */
        public static void main(String[] args) {
            // TODO 自動生成的方法存根
    
            System.out.println("Please input your score <0~100>:");
            Scanner in = new Scanner(System.in);
            double score = in.nextInt();
            grade(score);
        }
    
    }
  4. 輸入三個整數x,y,z,請把這三個數由小到大輸出(知識點:條件語句) [必作題]

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:ThreeNumberSort   
     * @Description:TODO 
     * @author: Ryanjie
     * @date:2018年8月8日 下午8:36:46   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class ThreeNumberSort {
    
    //  /**   
    //   * @Title: swap   
    //   * @Description:Swap two numbers
    //   * 
    //   * @param num1
    //   * @param num2      
    //   * @return: void      
    //   * @throws   
    //   */
    //  public static void swap(double num1, double num2) {
    //      double temp;
    //      temp = num1;
    //      num1 = num2;
    //      num2 = temp;
    //  }
    //  
    //  /**   
    //   * @Title: sort   
    //   * @Description: Three numbers sort
    //   * 
    //   * @param number1
    //   * @param number2
    //   * @param number3      
    //   * @return: void      
    //   * @throws   
    //   */
    //  public static void sort(double number1, double number2, double number3) {
    //      if (number1 < number2) {
    //          swap(number1, number2);
    //      }
    //      if (number1 < number3) {
    //          swap(number1, number3);
    //      }
    //      if (number2 < number3) {
    //          swap(number2, number3);
    //      }
    //      System.out.printf(number3 + " > " + number2 + " > " + number1);
    //  }
    
        public static void main(String[] args) {
    
            while(true) {
                System.out.println("Please input three numbers:");
                Scanner in = new Scanner(System.in);
                double number1 = in.nextInt();
                double number2 = in.nextInt();
                double number3 = in.nextInt();
    
                if (number1 < number2) {
                    double temp = number1;
                    number1 = number2;
                    number2 = temp;
                }
                if (number1 < number3) {
                    double temp = number1;
                    number1 = number3;
                    number3 = temp;
                }
                if (number2 < number3) {
                    double temp = number2;
                    number2 = number3;
                    number3 = temp;
                }
                System.out.printf(number1 + " > " + number2 + " > " + number3);
                System.out.println();
    
            }
    
        }
    
    }
  5. 有一個很少於5位的正整數,求它是幾位數,分別打印出每一位數字。(知識點:條件語句) [必作題]

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:PrintBitDigitNumber   
     * @Description:print a 5 digit number for each bit 
     * @author: Ryanjie
     * @date:2018年8月9日 上午12:12:01   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class PrintBitDigitNumber {
    
        /**   
         * @Title: getNumLength   
         * @Description:  get the number length
         * 
         * @param number      
         * @return: void      
         * @throws   
         */
        public static void getNumLength(long number) {
            System.out.println("The number<" + number + "> has " + (int)(Math.log10(number) + 1) + " bits");
        }
    
        /**   
         * @Title: divideBitNumber   
         * @Description: divide the number each bit
         * 
         * @param number      
         * @return: void      
         * @throws   
         */
        public static void divideBitNumber(long number) {
            int length = (int)(Math.log10(number));
            int highBit = 1;
            for(int i = 0; i < length; ++i) {
                highBit *= 10;
            }
    
            System.out.print("From high to low ,each bit is ");
            while (number > 0) {
                System.out.print(number/highBit + ",");
                number -= (number/highBit)*highBit;
                highBit /= 10;
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
            while (true) {
                System.out.println("Please input a number<long>: ");
                Scanner in = new Scanner(System.in);
                long number = in.nextInt();
                getNumLength(number);
                divideBitNumber(number);
                System.out.println();
            }
        }
    
    }
  6. 編寫一個程序,計算郵局匯款的匯費。若是匯款金額小於100元,匯費爲一元,若是金額在100元與5000元之間,按1%收取匯費,若是金額大於5000元,匯費爲50元。匯款金額由命令行輸入。(知識點:條件語句) [選作題]

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:RemittanceFee   
     * @Description:remittance fee 
     * @author: Ryanjie
     * @date:2018年8月9日 上午1:15:01   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class RemittanceFee {
    
        /**   
         * @Title: sumFee   
         * @Description: remittance fee
         * 
         * @param amount      
         * @return: void      
         * @throws   
         */
        public static void sumFee(double amount) {
            double sum;
            if (amount < 100) {
                sum = 1.0;
            }
            else if (amount <= 5000) {
                sum  = 0.01 * amount;
            }
            else {
                sum = 50.0;
            }
            System.out.println("The remittance fee is " + sum +"(RMB)");
        }
    
        public static void main(String[] args) {
            while (true) {
                System.out.println("Please input the amount<double>:");
                Scanner in = new Scanner(System.in);
                double amount = in.nextInt();
                sumFee(amount);
                System.out.println();
            }
        }
    
    }

0x03. 題13-17

  1. 分別使用for循環,while循環,do循環求1到100之間全部能被3整除的整數的和。(知識點:循環語句) [選作題]

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:SumOfNumbersDivisibleByThree   
     * @Description: the sum of the numbers that can be divisible by three
     * @author: Ryanjie
     * @date:2018年8月8日 下午9:16:17   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class SumOfNumbersDivisibleByThree {
    
        /**   
         * @Title: sumDivisibleByThree   
         * @Description: the sum of the numbers that can be divisible by three
         * 
         * @param num1
         * @param num2      
         * @return: void      
         * @throws   
         */
        public static void sumDivisibleByThree(int num1,int num2) {
            int sum = 0;
            for(int i = num1; i <= num2; i++) {
                if(i % 3 == 0) {
                    sum += i;
                }
            }
            System.out.println(sum);
    
        }
    
        public static void main(String[] args) {
            System.out.println("");
            Scanner in = new Scanner(System.in);
    //      int start = in.nextInt();
    //      int end = in.nextInt();
            int start = 1;
            int end = 100;
            sumDivisibleByThree(start, end);
    
        }
    
    }
  2. 輸出0-9之間的數,可是不包括5。 [選作題]

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:PrintfNineNumbers   
     * @Description:printf 0-9 apart from 5
     * @author: Ryanjie
     * @date:2018年8月8日 下午9:25:06   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class PrintfNineNumbers {
    
        /**   
         * @Title: printfNumbers   
         * @Description: printf 0-9 apart from 5
         * 
         * @param number1
         * @param number2      
         * @return: void      
         * @throws   
         */
        public static void printfNumbers(int number1, int number2) {
            int temp = 0;
            for(int i = number1; i <= number2; i++) {
                if (i != 5) {
                    System.out.printf(i + "\t");
                    if((++ temp) % 5 == 0) {
                        System.out.println();
                    }
                }
            }
        }
        public static void main(String[] args) {
            System.out.println("");
            Scanner in = new Scanner(System.in);
    //      int start = in.nextInt();
    //      int end = in.nextInt();
            int start = 0;
            int end = 9;
            printfNumbers(start, end);
        }
    
    }
  3. 編寫一個程序,求整數n的階乘,例如5的階乘是12345 [選作題]

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:Factorial   
     * @Description:Factorial
     * @author: Ryanjie
     * @date:2018年8月8日 下午9:59:47   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class Factorial {
    
        /**   
         * @Title: factorialOfN   
         * @Description: Factorial of n
         * 
         * @param n      
         * @return: void      
         * @throws   
         */
        public static void factorialOfN(int n) {
            int sum = 1;
            for(int i = 1; i <= n; ++i) {
                sum *= i;
            }
            System.out.printf("Factorial of " + n + " is " + 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();
            factorialOfN(number);
    
        }
    
    }
  4. 編寫一個程序,找出大於200的最小的質數[選作題]

    package com.ryanjie.test;
    
    /**   
     * @ClassName:Prime   
     * @Description:找出大於200的最小的質數
     * @author: Ryanjie
     * @date:2018年8月8日 下午3:02:18   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class Prime {
    
        /**   
         * @Title: isPrime   
         * @Description: 判斷一個數是否爲質數
         * 
         * @param num 
         * @return: boolean      
         * @throws   
         */
        public static boolean isPrime(long 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;
        }
    
        /**   
         * @Description: 找出大於200的最小的質數 
         */
        public static void main(String[] args) {
            int start = 200;
            int end = 300;
            int temp = 0;
            for(int i = start; i <= end; ++i) {
                if(isPrime(i)) {
                    System.out.printf(i + "\t");
                    if ((++ temp) % 10 ==0) {
                        System.out.println();
                    }
                }
    
            }
    
        }
    
    }
  5. 由命令行輸入一個4位整數,求將該數反轉之後的數,如原數爲1234,反轉後的數位4321 [選作題]

    package com.ryanjie.test;
    
    import java.util.Scanner;
    
    /**   
     * @ClassName:ReverseNumer   
     * @Description:reverse number 
     * @author: Ryanjie
     * @date:2018年8月9日 上午1:27:24   
     *     
     * @Copyright: 2018 Ryanjie Inc. All rights reserved. 
     */
    public class ReverseNumer {
    
        /**   
         * @Title: reverseMethod1   
         * @Description:reverse method1
         * 
         * @param number      
         * @return: void      
         * @throws   
         */
        public static void reverseMethod1(long number) {
            System.out.print("Method1:");
            while(number > 0) {
                System.out.print(number % 10);
                number /= 10;
            }
            System.out.println();
        }
    
        /**   
         * @Title: reverseMethod2   
         * @Description: reverse method2
         * 
         * @param number      
         * @return: void      
         * @throws   
         */
        public static void reverseMethod2(long number) {
            System.out.print("Method2:");
            String reserveNumber = "";
            while(number > 0) {
                reserveNumber += (number % 10);
                number /= 10;
            }
            System.out.println(reserveNumber);
        }
    
        public static void main(String[] args) {
            while (true) {
                System.out.println("Please input a number<long>:");
                Scanner in = new Scanner(System.in);
                long number = in.nextInt();
                reverseMethod1(number);
                reverseMethod2(number);
                System.out.println();
            }
        }
    
    }
相關文章
相關標籤/搜索