我是编程的新手,我需要你的帮助...我有一个名为"typeLabel"的标签,其默认标签文字为"NiMH".此文本通过pickerView更改为NiCad.我希望通过if语句来更改乘数值但是"unused variable floatTime"会暂停程序.代码是:
-(IBAction)calculate:(id)sender {
if ([typeLabel.text isEqualToString:@"NiCad"])
{
float floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.4;
} else {
float floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.5;
}
int intHourhs=(floatTime);
float floatHours=(intHourhs);
float floatMinutes=(floatTime-floatHours)*60;
NSString *stringTotal = [NSString stringWithFormat: @"%1.1i Hours and %.0f Minutes", intHourhs, floatMinutes];
chargeLabel.text=stringTotal;
}
Run Code Online (Sandbox Code Playgroud)
您需要在if语句上方移动声明:
float floatTime;
if ([typeLabel.text isEqualToString:@"NiMH"])
{
floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.5;
} else {
floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.4;
}
Run Code Online (Sandbox Code Playgroud)
在范围内声明局部变量时,在该范围之外不可见.