'if'语句有多个条件

时间:2012-10-13 19:41:21

标签: objective-c ios if-statement

所以我正在制作转换器应用程序,在使用if语句计算我的答案时遇到了问题。我有UIPickerWheel选择convertFromconvertTo,以及textField输入。

我的问题是,使用下面提到的代码,答案标签只显示随机单位,而不是pickerWheel中选择的单位。

我认为我的错是if语句没有检查两者是否相同,只是其中之一。如何检查两者是否为真,或以正确的方式进行检查?

Download for full project

代码:

#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
  inComponent:(NSInteger)component
{
    float convertFrom = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue];
    float convertTo = [[_convertRates objectAtIndex:[pickerView selectedRowInComponent:1]] floatValue];
    float input = [inputText.text floatValue];
    float to = convertTo;
    float from = convertFrom;
    float convertValue = input;
    float mtpaTilMtpaFloat = convertValue * 1;
    float mtpaTilMMcfdayFloat = convertValue * 2;
    float mtpaTilMillSm3dayFloat = convertValue * 3;
    float mtpaTilMMBTUFloat = convertValue * 4;
    float mtpaTilPJPAFloat = convertValue * 5;
    float MMcfdayTilmtpaFloat = convertValue * 0.5;
    float MMcfdayTilMMcfdayFloat = convertValue * 1;
    float MMcfdayTilMillSm3dayFloat = convertValue *6;
    float MMcfdayTilMMBTUFloat = convertValue *7;
    float MMcfdayTilPJPAFloat = convertValue *8;
    float MillSm3dayTilMTPAFloat = convertValue /3;

    NSString *mtpaTilmtpa = [[NSString alloc ] initWithFormat:
                             @" %f MTPA = %f MTPA", convertValue, mtpaTilMtpaFloat];

    NSString *mtpaTilMMcfday = [[NSString alloc ] initWithFormat:
                                @" %f MTPA = %f MMcf/day", convertValue, mtpaTilMMcfdayFloat];

    NSString *mtpaTilMillSm3day = [[NSString alloc] initWithFormat:
                                   @" %f MTPA = %f Mill.SM3/day", convertValue, mtpaTilMillSm3dayFloat];

    NSString *mtpaTilMMBTU = [[NSString alloc] initWithFormat:
                              @" %f MTPA = %f MMBTU", convertValue, mtpaTilMMBTUFloat];

    NSString *mtpaTilPJPA = [[NSString alloc] initWithFormat:
                             @" %f MTPA = %f PJPA", convertValue, mtpaTilPJPAFloat];



    NSString *MMcfdayTilmtpa = [[NSString alloc] initWithFormat:
                                @" %f MMcfday = %f MTPA", convertValue, MMcfdayTilmtpaFloat];

    NSString *MMcfdayTilMMcfday = [[NSString alloc] initWithFormat:
                                   @" %f MMcfday = %f MMcfday", convertValue, MMcfdayTilMMcfdayFloat];

    NSString *MMcfdayTilMillSm3day = [[NSString alloc] initWithFormat:
                                      @" %f MMcfday = %f MillSm3day", convertValue, MMcfdayTilMillSm3dayFloat];

    NSString *MMcfdayTilMMBTU = [[NSString alloc] initWithFormat:
                                 @" %f MMcfday = %f MMBTU", convertValue, MMcfdayTilMMBTUFloat];

    NSString *MMcfdayTilPJPA = [[NSString alloc] initWithFormat:
                                @" %f MMcfday = %f PJPA", convertValue, MMcfdayTilPJPAFloat];


    NSString *MillSm3dayTilMTPA = [[NSString alloc] initWithFormat:
                                   @" %f MillSm3day = %f MTPA", convertValue, MillSm3dayTilMTPAFloat];


    if (from = 1, to == 1) { resultLabel.text = mtpaTilmtpa;
    }
    if (from = 1, to == 2) { resultLabel.text = mtpaTilMMcfday;
    }
    if (from = 1, to == 3) { resultLabel.text = mtpaTilMillSm3day;
    }
    if (from = 1, to == 4) { resultLabel.text = mtpaTilMMBTU;
    }
    if (from = 1, to == 5) { resultLabel.text = mtpaTilPJPA;
    }
    if (from = 2, to == 1) { resultLabel.text = MMcfdayTilmtpa;
    }
    if (from = 2, to == 2) { resultLabel.text = MMcfdayTilMMcfday;
    }
    if (from = 2, to == 3) { resultLabel.text = MMcfdayTilMillSm3day;
    }
    if (from = 2, to == 4) { resultLabel.text = MMcfdayTilMMBTU;
    }
    if (from = 2, to == 5) { resultLabel.text = MMcfdayTilPJPA;
    }
    if (from = 3, to == 1) { resultLabel.text = MillSm3dayTilMTPA;
    }
}

感谢。

2 个答案:

答案 0 :(得分:2)

在大多数陈述中,您只有一个=。你应该使用双等号(==)。单个=将值分配给from,而不是检查是否相等。我还会将,替换为&&。最后要做的是组合if语句的公共部分。

if (from == 1) { 
    if (to == 1) {
        resultLabel.text = mtpaTilmtpa;
    }
    else if (to == 2) { 
        resultLabel.text = mtpaTilMMcfday;
    }
    else if (to == 3) { 
        resultLabel.text = mtpaTilMillSm3day;
    }
    else if (to == 4) { 
        resultLabel.text = mtpaTilMMBTU;
    }
    else if (to == 5) { 
        resultLabel.text = mtpaTilPJPA;
    }
}
else if (from == 2) { 
    if (to == 1) {
        resultLabel.text = MMcfdayTilmtpa;
    }
    else if (to == 2) { 
        resultLabel.text = MMcfdayTilMMcfday;
    }
    else if to == 3) { 
            resultLabel.text = MMcfdayTilMillSm3day;
    }
    else if (to == 4) { 
        resultLabel.text = MMcfdayTilMMBTU;
    }
    else if (to == 5) { 
        resultLabel.text = MMcfdayTilPJPA;
    }
}
else if (from == 3 && to == 1) { 
    resultLabel.text = MillSm3dayTilMTPA;
}

答案 1 :(得分:1)

您对所有这些if语句的使用不能很好地扩展。您应该有一个包含标签的数组数组。然后使用两个组件中的选定选取器行来访问阵列中的元素。然后,所有if语句都缩减为3行代码。