0

我是 Objective C 的新手。我在 2011-12 年跟随斯坦福大学讲授 iOS 开发,并在作业 1 中要求在计算器中实现小数点。这就是我的实现的样子:

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

@interface CalculatorViewController()

@property (nonatomic) BOOL userIsInTheMiddleOfEnteringNumber;
@property (nonatomic,strong) CalculatorBrain *brain;
@property (nonatomic) int userPressedDecimalPoint;
@end

@implementation CalculatorViewController
@synthesize display = _display;
@synthesize userIsInTheMiddleOfEnteringNumber = _userIsInTheMiddleOfEnteringNumber;
@synthesize userPressedDecimalPoint = _userPressedDecimalPoint; 
@synthesize brain = _brain;


- (CalculatorBrain *) brain{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}

- (IBAction)digitpressed:(UIButton *)sender {

NSString *digit = sender.currentTitle;
if (digit == @"."){
    self.userIsInTheMiddleOfEnteringNumber = YES;
    self.userPressedDecimalPoint++;
}

if (self.userIsInTheMiddleOfEnteringNumber &&  self.userPressedDecimalPoint< 2){
    self.display.text = [self.display.text stringByAppendingString:digit];
    NSLog(@"decimal pressed: %d times",self.userPressedDecimalPoint);
} 
else{
    self.display.text = digit;
    self.userIsInTheMiddleOfEnteringNumber = YES;
    self.userPressedDecimalPoint = 1;
}
}

所以基本上我已经设置了一个属性 userPressedDecimalPoint 并且每次按下小数点时我都尝试使用这个属性作为计数器。但是,从 NSLog 中我看到,无论我按小数点多少次,它都只显示 1 次按下。因此,如果输入,输出会显示多个小数点。任何建议,将不胜感激。谢谢!

完整版更正代码:

#import "CalculatorViewController.h"
#import "CalculatorBrain.h"

@interface CalculatorViewController()

@property (nonatomic) BOOL userIsInTheMiddleOfEnteringNumber;
@property (nonatomic,strong) CalculatorBrain *brain;
@property (nonatomic) int userPressedDecimalPoint;
@end

@implementation CalculatorViewController
@synthesize display = _display;
@synthesize userIsInTheMiddleOfEnteringNumber = _userIsInTheMiddleOfEnteringNumber;
@synthesize userPressedDecimalPoint = _userPressedDecimalPoint;
@synthesize brain = _brain;


- (CalculatorBrain *) brain{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}

- (IBAction)digitpressed:(UIButton *)sender {

NSString *digit = sender.currentTitle;
if ([digit isEqualToString:@"."]){
    self.userIsInTheMiddleOfEnteringNumber = YES;
    self.userPressedDecimalPoint++;
}

if (self.userIsInTheMiddleOfEnteringNumber &&  self.userPressedDecimalPoint< 2){
    self.display.text = [self.display.text stringByAppendingString:digit];
   // NSLog(@"decimal pressed: %d times",self.userPressedDecimalPoint);
} 
else if (![digit isEqualToString:@"."]){
    self.display.text = digit;
    self.userIsInTheMiddleOfEnteringNumber = YES;
}
}

- (IBAction)operationPressed:(UIButton *)sender {
if (self.userIsInTheMiddleOfEnteringNumber) [self enterPressed];
double result = [self.brain performOperation:sender.currentTitle];
NSString *resultString = [NSString stringWithFormat:@"%g", result];
self.display.text = resultString;

}

- (IBAction)enterPressed {
self.userPressedDecimalPoint = 0;
[self.brain pushOperand:[self.display.text doubleValue]];
self.userIsInTheMiddleOfEnteringNumber = NO;
} 

@end
4

1 回答 1

0

除非有设置userIsInTheMiddleOfEnteringNumberNO,否则只要数字大于一,您的代码就会执行self.userPressedDecimalPoint = 1;并将其设置回一。

于 2012-07-29T15:20:40.060 回答