对于这样一个愚蠢的问题,我很抱歉 我正在根据分段控件的索引更改变量值,但后来想在后面的计算中使用此变量;我确定这与变量范围有关吗?
- (IBAction)calculate:(UIButton *)button {
if( [sSeg selectedSegmentIndex]==1){
float s=0.5;
NSLog(@"s=%f", s);
}
else if ([sSeg selectedSegmentIndex]==0)
{
float s=1;
NSLog(@"s=%f", s);
}
NSLog(@”s now = %f”, s);
}
非常感谢!
答案 0 :(得分:4)
- (IBAction)calculate:(UIButton *)button {
float s = 0;
if( [sSeg selectedSegmentIndex]==1){
s=0.5;
NSLog(@"s=%f", s);
}
else if ([sSeg selectedSegmentIndex]==0)
{
s=1;
NSLog(@"s=%f", s);
}
NSLog(@”s now = %f”, s);
}
是的,它的范围 - 变量只在大括号内可见。
答案 1 :(得分:0)
- (IBAction)calculate:(UIButton *)button {
float s;
if( [sSeg selectedSegmentIndex]==1){
s=0.5;
NSLog(@"s=%f", s);
}
else if ([sSeg selectedSegmentIndex]==0)
{
s=1;
NSLog(@"s=%f", s);
}
NSLog(@”s now = %f”, s);
}