Java语言程序设计-基础篇-第八版-编程练习题-第五章

时间:2021-07-28 02:04:04
 1 package 编程练习题chapter5;
 2 import java.util.*;  //我改了;
 3 public class Exercise5_1 {
 4     public static void main (String args[]) {
 5         Scanner input = new Scanner(System.in);
 6         
 7         System.out.print("Enter a number: ");
 8         int number = input.nextInt();
 9         
10         for (int i = 1; i <= number; i++)
11                 getPentagonalNumber(i);
12         }
13     
14      public static void getPentagonalNumber(int n){
15          if (n % 10 == 0)
16              System.out.println(n * (3 * n - 1) / 2);
17          else
18              System.out.printf("%10d", n * (3 * n - 1) / 2);
19         
20     }
21 }
package 编程练习题chapter5;

public class Exercise5_2 {
    public static void main(String args[]){
        java.util.Scanner input = new java.util.Scanner(System.in);
        
        System.out.print("Enter an interger: ");
        long value = input.nextInt();
        System.out.println(sumDigits(value));
    }
    
    public static  int sumDigits(long n){
        int temp = (int)Math.abs(n);
        int sum = 0;
        
        while (temp != 0) {
            int remainder = temp % 10;
            sum += remainder;
            temp = temp / 10;
        }
        return sum;
    }

}
 1 package 编程练习题chapter5;
 2 //回文整数,例如234变成432,不一样不是回文数。252就是回文数。
 3 public class Exercise5_3 {
 4     public static void main (String args[]){
 5        java.util.Scanner input = new java.util.Scanner(System.in);
 6        
 7        System.out.print("Enter a postive integer: ");
 8        int number = input.nextInt();
 9        
10        if (isPalindrome(number))
11            System.out.println(number + " is palindrome");
12        else
13            System.out.println(number + " is not palindrome");
14     }
15     
16     public static boolean isPalindrome(int number) {
17         return number == reverse(number);
18     }
19     
20     public static int reverse(int number) {
21         int result = 0;
22         while (number != 0) {
23             int remainder = number % 10;
24             result = result * 10 + remainder;
25             number = number / 10;
26         }
27         return result;
28     }
29 }
 1 package 编程练习题chapter5;
 2 
 3 public class Exercise5_4 {
 4     public static void main (String args[]){
 5            java.util.Scanner input = new java.util.Scanner(System.in);
 6            
 7            System.out.print("Enter an integer: ");
 8            int number = input.nextInt();
 9            reverse(number);
10         }
11         
12         
13         public static void reverse(int number) {
14             while (number != 0) {
15                 int remainder = number % 10;
16                 System.out.print(remainder);
17                 number = number / 10;
18             }
19             
20             System.out.println();
21         }
22     }
 1 package 编程练习题chapter5;
 2 
 3 public class Exercise5_6 {
 4     public static void main(String[] args){
 5         java.util.Scanner input = new java.util.Scanner(System.in);
 6         System.out.print("Enter line number: ");
 7         int lineNumber = input.nextInt();
 8         
 9         displayPattern(lineNumber);
10     }
11     
12     public static void displayPattern(int n) {
13         for (int row = 1; row <= n; row++) {
14             
15             for (int i = row; i < n; i++)
16                 System.out.print(" ");
17             
18             for (int i = row; i >= 1; i--)
19                 System.out.print(" " + i);
20             
21             System.out.println();
22                 
23         }
24     }
25 }
 1 package 编程练习题chapter5;  //回文素数
 2 
 3 public class Exercise5_26 {
 4     public static void main(String args[]){
 5         int count = 1;
 6         
 7         for (int i = 2; true; i++){
 8             if (isPrime(i) && isPalindrome(i)){
 9                 System.out.print(i + " ");
10                 
11                 if (count % 10 == 0){
12                     System.out.println();
13                 }
14                 
15                 if (count == 100){
16                     break;
17                 }
18                 
19                 count++;
20             }
21         }
22     }
23     
24     public static boolean isPrime(int num){
25         if ((num == 1) || (num == 2)){         
26             return true;
27         }
28         
29         for (int i = 2; i <= num / 2; i++){
30             if (num % i == 0){
31                 return false;
32             }
33         }
34         
35         return true;
36     }
37     
38     static int reversal(int num){
39         int result = 0;
40         
41         while (num != 0){
42             int lastDigit = num % 10;
43             result = result * 10 + lastDigit;
44             num = num / 10;
45         }
46         
47         return result;
48     }
49     
50     static boolean isPalindrome(int num){
51         return num == reversal(num);
52     }
53 
54 }
 1 package 编程练习题chapter5;
 2 
 3 public class Exercise5_33 {
 4     public static void main (String args[]){
 5        long seconds = System.currentTimeMillis() / 1000;
 6        long currentSecond = seconds % 60;
 7        long minutes = seconds / 60;
 8        long currentMinute = minutes % 60;
 9        long hours = minutes / 60;
10        long currentHour = hours % 24;
11        long days = hours / 24 + 1;
12    
13        int year = 1970;  //get year;
14        while (days >= (isLeapYear(year) ? 366 : 365)) {
15           days = days - (isLeapYear(year) ? 366 : 365);
16           year++;
17        }
18    
19        int month = 1;  //get month;
20        while (days >= getNumberOfDaysInMonth(year, month)) {
21           days = days - getNumberOfDaysInMonth(year, month);
22           month++;
23        }
24    
25        System.out.println("Current data and time is " +
26        getMonthName(month) + " " + days + "," + year + " " +
27        currentHour + ":" + currentMinute + ":" + currentSecond);
28     }
29     
30     public static String getMonthName(int month) {
31         String monthName = null;
32         switch (month) {
33           case 1: monthName = "January"; break;
34           case 2: monthName = "February"; break;
35           case 3: monthName = "March"; break;
36           case 4: monthName = "April"; break;
37           case 5: monthName = "May"; break;
38           case 6: monthName = "June"; break;
39           case 7: monthName = "July"; break;
40           case 8: monthName = "August"; break;
41           case 9: monthName = "September"; break;
42           case 10: monthName = "October"; break;
43           case 11: monthName = "November"; break;
44           case 12: monthName = "December";
45         }
46         
47         return monthName;
48     }
49     
50     public static int getNumberOfDaysInMonth(int year, int month) {
51         if (month == 1 || month == 3 || month == 5 || month == 7 ||
52           month == 8 || month == 10 || month == 12)
53            return 31;
54 
55           if (month == 4 || month == 6 || month == 9 || month == 11)
56              return 30;
57 
58            if (month == 2) return isLeapYear(year) ? 29 : 28;
59 
60            return 0;
61     }
62     
63     public static boolean isLeapYear(int year) {
64         return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
65     }
66 }