0

所以我有一个方法可以在按下按钮时运行,一切都很完美,除了我有一点内部 if/else if/else 循环。我确定这是我想念的愚蠢的东西,但我似乎看不到它。

在下面的代码中,我找到了小时类型,但即使我直接将其设置为 false,if/else 也不会触发。它能够很好地获得小时 int,但它不会像预期的那样从中减去 12。

我知道我没有在这里指定日期类型,因为我之前已经这样做了。这不是这里的问题。就像我说的那样,我确定我错过了一些愚蠢的东西,因为我已经盯着它太久了。这是方法:

public String enterMood(View v) {
    try {
        int month = dPick.getMonth();
        int day = dPick.getDayOfMonth();
        int year = dPick.getYear();
        int minute = tPick.getCurrentMinute();
        String moodAntePost = "AM";
        hourType = tPick.is24HourView();
        moodHour = tPick.getCurrentHour();
        if (hourType = false) { // Not hitting this point for some reason I
                                // can't fathom.
            if (moodHour > 12) {
                moodHour = (moodHour - 12);
                moodAntePost = "PM";
            }
        } else if (hourType = false) {
            if (moodHour <= 0) {
                moodHour = 12;
            }
        } else {
        }
        String noteText = noteField.getText().toString();
        Mood = "Happiness," + happyValue + ",Energy," + energyValue
                + ",Anxiety," + anxietyValue + ",Pain," + painValue
                + ",Date," + month + "/" + day + "/" + year + ",Time,"
                + moodHour + ":" + minute + "," + moodAntePost + ",Note,"
                + noteText;
        System.out.println(Mood); //Just to print to the LogCat
    } catch (Exception buttonListenerException) {
        Log.e(TAG, "Exception received", buttonListenerException);
    }
    return Mood;
}
4

3 回答 3

4

澄清:=用于分配目的,例如int x = 10;==用于比较,例如boolean isX10 = x==10;

你的if说法是错误的这样做:

 if (hourType == false) { // Not hitting this point for some reason I
                            // can't fathom.

或者

 if (!hourType) { // Not hitting this point for some reason I
                            // can't fathom.

代替

 if (hourType = false) { // Not hitting this point for some reason I
                            // can't fathom.
于 2013-02-28T14:08:18.147 回答
2

也许hourType = false应该hourType == false或者甚至更好!hourType

于 2013-02-28T14:08:08.393 回答
0

if 和 else 如果两者都以错误的语法检查相同的条件,它应该类似于

 if (hourType == false) { // Not hitting this point for some reason I
                                    // can't fathom.
                if (moodHour > 12) {
                    moodHour = (moodHour - 12);
                    moodAntePost = "PM";
                }
            } else if (hourType == true) {
                if (moodHour <= 0) {
                    moodHour = 12;
                }

或者

 if (!hourType) { // Not hitting this point for some reason I
                                        // can't fathom.
                    if (moodHour > 12) {
                        moodHour = (moodHour - 12);
                        moodAntePost = "PM";
                    }
                } else if (hourType) {
                    if (moodHour <= 0) {
                        moodHour = 12;
                    }
于 2013-02-28T14:11:52.303 回答