[Java練習](2)閏年判斷練習

題目內容:給兩整數一為年份,一為月份,判斷該年是否為閏年及該月有幾天

閏年判斷規則:可以被 4 整除的年份為閏年:例如,1988、1992 及 1996 均為閏年。 然而,仍有一個小誤差必須列入考量。為了消除這個誤差,西曆規定,可以被 100 整除的年份 (例如 1900) 必須同時被 400 整除才是閏年。

基於這個原因,下列年份為非閏年: 1700、1800、1900、2100、2200、2300、2500、2600 這是因為這些年份可被 100 整除,但不能被 400 整除。

下列年份「均為」閏年:
1600, 2000, 2400
這是因為它們可以同時被 100 和 400 整除。

    int month = 2;
    int leapyear = 2019 ;

    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        System.out.println(month + " has 31 days.");
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        System.out.println(month + " has 30 days.");
    } else if (month == 2) {
        //...leap year.
        if(leapyear%4==0){
            //先判斷此年份能不能被4整除
            if (leapyear%100==0 && leapyear%400!=0){
                //被100整除的年份要能被400同時整除才可以是閏年
                System.out.println(leapyear + "此年份不為閏年,2月有28天"); }
            else{
                System.out.println(leapyear + "此年份為閏年,2月有29天"); }
        }else { //不能被4整除的數都不是閏年 
            System.out.println("此年不為閏年,2月有28天");
        }
    } else {
        System.out.println("Invalid Month");}
        System.out.println("finished");}

Output:

此年不為閏年,2月有28天
finished

[Java練習](1)攝氏華氏溫度轉換

題目:任給一整數degree 和 index
index 為1時攝氏轉華氏
index 為2時華氏轉攝氏
並列印出來
攝氏(C)華氏(F)轉換演算法:F = ( C*9/5)+32

public static void main(String[] args) {
    int degree = 100;
    int index = 1; 
    float Newdegree;
    Newdegree =(float) degree;//將度數強制轉型成float 
    if(index==1) { //當index = 1的情況 把攝氏度數轉華氏
        System.out.println("華氏度數為"+((Newdegree*9/5)+32)+"度");
    } 
    else if(index==2) {//當index = 2的情況 把華氏度數轉攝氏
        System.out.println("攝氏度數為"+((Newdegree-32)*5/9)+"度");
    }
}

Output:華氏度數為212.0度

[HackerRanker](7)Java Date and Time

問題網址:https://www.hackerrank.com/challenges/java-date-and-time/problem

問題重點:Java API中 Java.time的使用

JavaSE8後可使用 import java.time.*;

Class LocalDate 1.方法of:

public static LocalDate of(int year,
                           int month,
                           int dayOfMonth)

2.同類別 方法getDayOfWeek取得回傳列舉Enum DayOfWeek中的星期(
The singleton instance for the day-of-week of Friday. )

getDayOfWeek

public DayOfWeek getDayOfWeek()

3.同類別方法toString將Enum轉成字串回傳

toString

public String toString()

[HackerRanker](6)Java Int to String

問題網址:https://www.hackerrank.com/challenges/java-int-to-string/problem

問題重點:型別轉換函數

查詢API Java.lang class Integer 輸出String

static 方法 使用: 類別名稱.方法名稱();

https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html

toString

public static String toString(int i)

Returns a String object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to the toString(int, int) method.Parameters:i – an integer to be converted.Returns:a string representation of the argument in base 10.

解答: String s = Integer.toString(n);

[HackerRank](1)Java Output Formatting

題目網址 https://www.hackerrank.com/challenges/java-output-formatting/problem?h_r=next-challenge&h_v=zen

練習主軸:輸出格式化

轉換符說明示例
%s字符串類型“mingrisoft"
%c字符類型‘m’
%b布爾類型true
%d整數類型(十進制)99
%x整數類型(十六進制)FF
%o整數類型(八進制)77
%f浮點類型99.99
%a十六進制浮點類型FF.35AE
%e指數類型9.38e+5
%g通用浮點類型(f和e類型中較短的)
%h散列碼
%%百分比類型%
%n換行符
%tx日期與時間類型(x代表不同的日期與時間轉換符)

%-15s : -號代表從左邊輸入字串(若不寫則預設為向右對齊)。15為指定長度 s為字串

%03d:需要3位數10進位數字並向左補零

範例解答:

[Java]物件導向關鍵字(1)static靜態

Static 靜態 相對於 instance 實體

使用Static 即只在程式一開始做一次預設初始化,不須使用new物件初始化,但缺點是一開始即佔據記憶體

靜態屬性宣告方法:

[Modifier] static type identifier [= value];

靜態方法宣告方法:

[Modifier] static return_type identifier([Parameter_Lists]){

statement;}

使用靜態方法:

方法類別名稱.方法名稱();

classname.staticfield; classname.staticmethod();