我从随机数字代码得到混合结果

时间:2014-05-10 14:25:48

标签: objective-c

对于我们被要求创建应用的课程项目。部分我的应用程序包括一个硬币计数应用程序,其中生成随机数量,并且用户需要点击硬币按钮,直到达到正确的金额。 这是.m代码......

//
//  CountingChangeViewController.m
//  Piggy  Bank
//

#import "CountingChangeViewController.h"

@interface CountingChangeViewController ()
//define button connections
- (IBAction)PennyButton:(id)sender;
- (IBAction)NickelButton:(id)sender;
- (IBAction)DimeButton:(id)sender;
- (IBAction)QuarterButton:(id)sender;
- (IBAction)ResetButton:(id)sender;

//define UILable connections
@property (weak, nonatomic) IBOutlet UILabel *labelAmountRequired;
@property (weak, nonatomic) IBOutlet UILabel *labelAmountInBank;
@property (weak, nonatomic) IBOutlet UILabel *labelAmountRemaining;
@property (weak, nonatomic) IBOutlet UILabel *labelCoinCount;
@property (weak, nonatomic) IBOutlet UIImageView *imagePickNewCoin;
@property (weak, nonatomic) IBOutlet UILabel *labelPennyCount;
@property (weak, nonatomic) IBOutlet UILabel *labelNickelCount;
@property (weak, nonatomic) IBOutlet UILabel *labelDimeCount;
@property (weak, nonatomic) IBOutlet UILabel *labelQuarterCount;


@end

//declare class variables
double AmountTotal = 0;
double AmountBank = 0;
double AmountRemaining = 0;
double rNumberMod;
double coinValue;
int coinCount = 0;
int pennyCount = 0;
int nickelCount = 0;
int dimeCount = 0;
int quarterCount = 0;


//start implementation
@implementation CountingChangeViewController

- (void)resetPage{
    _imagePickNewCoin.hidden=YES;
    //simply generates a new random amount
    AmountTotal = [self RandomNumber];
    _labelAmountRequired.text = [NSString stringWithFormat:@"$%.2f", AmountTotal];
    AmountBank = 0;
    _labelAmountInBank.text = [NSString stringWithFormat:@"$%.2f", AmountBank];
    AmountRemaining = 0;
    _labelAmountRemaining.text = [NSString stringWithFormat:@"$%.2f", AmountRemaining];
    coinCount = 0;
    _labelCoinCount.text = [NSString stringWithFormat:@"%d", coinCount];
    pennyCount = 0;
    _labelPennyCount.text = [NSString stringWithFormat:@"%d", pennyCount];
    nickelCount = 0;
    _labelNickelCount.text = [NSString stringWithFormat:@"%d", nickelCount];
    dimeCount = 0;
    _labelDimeCount.text = [NSString stringWithFormat:@"%d", dimeCount];
    quarterCount = 0;
    _labelQuarterCount.text = [NSString stringWithFormat:@"%d", quarterCount];

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self resetPage];
   }


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)PennyButton:(id)sender {

    pennyCount++;
    _labelPennyCount.text =[NSString stringWithFormat:@"%d",pennyCount];

    coinValue = .01;
    [self checkValue];

}

- (IBAction)NickelButton:(id)sender {

    if ((AmountRemaining > 0) && (AmountRemaining <= .04))
    {
        //display the error message
        _imagePickNewCoin.hidden=NO;
    }
    else
    {
        nickelCount++;
        _labelNickelCount.text =[NSString stringWithFormat:@"%d",nickelCount];

        coinValue = .05;
        [self checkValue];
    }
}

- (IBAction)DimeButton:(id)sender {

    if ((AmountRemaining > 0) && (AmountRemaining <= .09))
    {
        //display the error message
        _imagePickNewCoin.hidden=NO;
    }
    else
    {
        dimeCount++;
        _labelDimeCount.text =[NSString stringWithFormat:@"%d",dimeCount];

        coinValue = .10;
        [self checkValue];
    }
}

- (IBAction)QuarterButton:(id)sender {

    if ((AmountRemaining > 0) && (AmountRemaining <= .24))
    {
        //display the error message
        _imagePickNewCoin.hidden=NO;
    }
    else
    {
        quarterCount++;
        _labelQuarterCount.text =[NSString stringWithFormat:@"%d",quarterCount];
        coinValue = .25;
        [self checkValue];
    }
}

-(double)RandomNumber{
    int rNumber = arc4random_uniform(99) +1;
    double rNumberMod = rNumber * .01;
    return rNumberMod;
}

-(void)checkValue {
    //hide the error message
    _imagePickNewCoin.hidden=YES;

    //at button press play coin drop sound
    SystemSoundID soundID;
    NSString *soundFile = [[NSBundle mainBundle]
                           pathForResource :@"coin-drop-1" ofType :@"mp3"];

    AudioServicesCreateSystemSoundID ((__bridge CFURLRef)
                                      [NSURL fileURLWithPath :soundFile]
                                      , &soundID);
    AudioServicesPlaySystemSound (soundID);


    //add coinvalue to amountTotal and put the resulting value in the AmountInBank UILabel .text attribute

    //this is the math occuring behind the scenes
    AmountBank = AmountBank + coinValue;

    //this is how the math result shows on the screen
    _labelAmountInBank.text = [NSString stringWithFormat:@"$%.2f", AmountBank];



    //calculate the amountRemaining and put the resulting value in the AmountRemaining UILabel .text attribute

    //this is the math occuring behind the scenes
    AmountRemaining = AmountTotal - AmountBank;

    //this is how the math result shows on the screen
    _labelAmountRemaining.text = [NSString stringWithFormat:@"$%.2f", AmountRemaining];

    // increment coin count by one and display it on the screen
    coinCount++;
    _labelCoinCount.text =[NSString stringWithFormat:@"%d",coinCount];

    //test to see if the AmountRemaining = 0
    if (AmountRemaining == 0)
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"CONGRATULATIONS!" message:@"You saved the correct amount of money! Would you like to try again?" delegate:self cancelButtonTitle:nil otherButtonTitles:@"TRY AGAIN", nil];
        [alertView show];

    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0)
        [self resetPage];
}

- (IBAction)ResetButton:(id)sender {
    [self resetPage];
}

@end

有时它会起作用,有时却不起作用。当需要的更改量达到0时,它应该显示一个警报视图。有时它会这样做,有时则不会。此外,当amountRemaining小于点击的硬币按钮的值时,它应该显示隐藏的图像“imagePickNewCoin”,而不是将硬币的值添加到银行金额。有时这是有效的,有时它会执行计算,从而导致amountRemaining的负值。

为什么这有时会有效而不是其他时间?

0 个答案:

没有答案