2016-02-10 54 views
-2
public class EX02 { 

public static int currentDay; 
public static String result; 

/** 
* Constant. 
* Every 3 days, feed worms. 
*/ 
public static final int WORM_FEEDING_DAY = 3; 

/** 
* Constant. 
* Every 5 days, bathe in sand. 
*/ 
public static final int BATHING_DAY = 5; 

/** 
* Constant. 
* Total number of days for which instructions are needed. 
*/ 
public static final int NUMBER_OF_DAYS = 30; 

/** 
* Entry point of the program. 
* @param args Arguments from command line. 
*/ 
public static void main(String[] args) { 

    getInstructionForCurrentDay(currentDay); 
    // call and print getInstructionForCurrentDay inside a loop here 
    for (currentDay = 1; currentDay < NUMBER_OF_DAYS +1; currentDay++) { 
     currentDay = 30 - (NUMBER_OF_DAYS - currentDay); 

     System.out.println("DAY " + currentDay + result); 
    } 


    System.out.println("Can't fly back in time"); 

} 



/** 
* Return instruction for given day. 
* @param currentDay number of day to print instructions for. 
*/ 
public static String getInstructionForCurrentDay(int currentDay) { 

    if (currentDay % 3 == 0){ 
     result = ":feed worms"; 
    } 
    else if (currentDay % 5 == 0){ 
     result = ":time to bath"; 
    } 
    else if (currentDay % 5 == 0 && currentDay % 3 ==0 ){ 
     result = ":glide"; 
    } 
    else if (currentDay % 3 !=0 || currentDay % 5 != 0) { 
     result = ":give fruit and water"; 
    } 

    return result; 
} 
} 

問題是'getInstructionForCurrentDay(int currentDay)'在Main方法內不返回想要的值。我做錯了什麼?如果內部運算符沒有返回正確的值

+0

'字符串str = getInstructionForCurrentDay(CURRENTDAY); ' – Satya

+0

什麼是結果?它沒有在代碼示例中聲明... – fge

+0

@fge:它是 - 代碼格式錯誤:( –

回答

2

if (currentDay % 5 == 0 && currentDay % 3 ==0 ) 

條件應該先,否則它永遠無法達到。

除此之外,你應該的getInstructionForCurrentDay(currentDay);結果分配給一些變量,如果你想要那個方法的調用者使用它,你應該叫getInstructionForCurrentDay(currentDay)內循環,因爲currentDay只在循環內分配。

// call and print getInstructionForCurrentDay inside a loop here 
for (currentDay = 1; currentDay < NUMBER_OF_DAYS +1; currentDay++) { 
    currentDay = 30 - (NUMBER_OF_DAYS - currentDay); 
    result = getInstructionForCurrentDay(currentDay); 
    System.out.println("DAY " + currentDay + result); 
} 

... 

public static String getInstructionForCurrentDay(int currentDay) { 

    if (currentDay % 5 == 0 && currentDay % 3 ==0 ){ 
     result = ":glide"; 
    } 
    else if (currentDay % 3 == 0){ 
     result = ":feed worms"; 
    } 
    else if (currentDay % 5 == 0){ 
     result = ":time to bath"; 
    } 
    else { 
     result = ":give fruit and water"; 
    } 

    return result; 
} 
0

你傳入currentDay到你的方法被實例化之前,所以你從字面上傳遞0進去。

因此,您的所有if語句條件都不會被滿足,因此result也永遠不會被賦值,因此不會返回您想要的值。

相反,我相信你想這樣做:

for (currentDay = 1; currentDay < NUMBER_OF_DAYS +1; currentDay++) 
{   
    getInstructionForCurrentDay(currentDay); 
    System.out.println("DAY " + currentDay + result); 
    currentDay = 30 - (NUMBER_OF_DAYS - currentDay); 
} 
+0

'currentDay'只包含一個零值,而不是一個。 – peter

+0

是的,對不起,忽略了這樣的事實:一個整數不能爲空,並且默認爲0。 – James

0

謝謝,我解決了這種方式:

public class EX02 { 
/** 
* Constant. Every 3 days, feed worms. 
*/ 
public static final int WORM_FEEDING_DAY = 3; 
/** 
* Constant. Every 5 days, bathe in sand. 
*/ 
public static final int BATHING_DAY = 5; 
/** 
* Constant. Total number of days for which instructions are needed. 
*/ 
public static final int NUMBER_OF_DAYS = 30; 
/** 
* Entry point of the program. 
* 
* @param args 
*   Arguments from command line. 
*/ 
public static void main(String[] args) { 
    // call and print getInstructionForCurrentDay inside a loop here 
    if (NUMBER_OF_DAYS < 1) { 
     System.out.println("Can't fly back in time"); 
     System.exit(0); 
    } 
    for (int currentDay = 1; currentDay <= NUMBER_OF_DAYS; currentDay++) { 
     System.out.println("Day " + currentDay + ": " 
       + getInstructionForCurrentDay(currentDay)); 
    } 
} 
/** 
* Return instruction for given day. 
* 
* @param currentDay 
*   number of day to print instructions for. 
*/ 
public static String getInstructionForCurrentDay(int currentDay) { 
    String result = ""; 
    if (currentDay % WORM_FEEDING_DAY == 0 && currentDay % BATHING_DAY == 0) 
     result = "glide in wind"; 
    else if (currentDay % WORM_FEEDING_DAY == 0) 
     result = "feed worms"; 
    else if (currentDay % BATHING_DAY == 0) 
     result = "bathe in sand"; 
    else 
     return "give fruit and water"; 
    return result; 
} 

}