如果操作符内部方法没有返回正确的值

时间:2016-02-10 15:40:05

标签: java

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;
}
}

问题是&#39; getInstructionForCurrentDay(int currentDay)&#39;不会在Main方法中返回所需的值。我做错了什么?

3 个答案:

答案 0 :(得分:2)

你的

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

条件应该先行,否则永远无法达到。

除此之外,如果您希望该方法的调用者使用它,您应该将getInstructionForCurrentDay(currentDay);的结果分配给某个变量,并且您应该在循环内调用getInstructionForCurrentDay(currentDay),因为{{1} }仅在循环内分配。

currentDay

答案 1 :(得分: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);
}

答案 2 :(得分: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;
}

}