我正在为设计游戏的女儿做一个简单的程序。我只想让Mac拿出一个从1到6的随机数,并将其与用户的猜测进行比较,然后获得用于确定玩家在棋盘游戏中可以移动多少空间的差异。一切正常,除了程序生成的差异总是3.它正确生成随机数并正确读取用户的输入。这是代码。我真的很感激帮助。我是新手,意识到可能有一个非常简单的答案。我已经搜索和搜索,并没有拿出一个解决方案。有一点它正确地产生了差异,但现在不是。非常感谢!我会为新可可程序员欣赏帮助
//
// AstroGuessAppDelegate.h
//
// Created by Trent Evans on 3/7/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
int macPick;
int numberGuess;
int numberDiff;
@interface AstroGuessAppDelegate : NSObject {
IBOutlet NSWindow *window;
IBOutlet id moveResultLabel;
IBOutlet id thinkingLabel;
IBOutlet NSComboBox *numberGuessBox;
}
- (IBAction)compareNumbersAndSendResults:(id)sender;
- (IBAction)macThinkOfNumber:(id)sender;
@end
//
// AstroGuessAppDelegate.m
//
// Created by Trent Evans on 3/7/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "AstroGuessAppDelegate.h"
@implementation AstroGuessAppDelegate
- (IBAction)macThinkOfNumber:(id)sender {
macPick = (arc4random() % 6) + 1;
NSString *thinkingLabelText = [NSString stringWithFormat: @"Ok. I'm thinking of a number."];
[thinkingLabel setStringValue:thinkingLabelText];
}
- (IBAction)compareNumbersAndSendResults:(id)sender {
numberGuess = [numberGuessBox intValue];
numberDiff = macPick - numberGuess;
if (numberDiff<0) {
numberDiff = numberDiff * -1;
}
NSString *moveResultLabelText;
if (numberDiff=0) {
moveResultLabelText = [NSString stringWithFormat:@"My number was %i. Your number was %i. The difference is %i. \nBLAST OFF!\nMove forward 6 spaces", macPick, numberGuess, numberDiff];
}
if (numberDiff=1) {
moveResultLabelText = [NSString stringWithFormat:@"My number was %i. Your number was %i. The difference is %i. \nORBIT!\nMove forward 4 spaces", macPick, numberGuess, numberDiff];
}
if (numberDiff=2) {
moveResultLabelText = [NSString stringWithFormat:@"My number was %i. Your number was %i. The difference is %i. \nRE-ENTRY!\nMove forward 2 spaces", macPick, numberGuess, numberDiff];
}
if (numberDiff=3) {
moveResultLabelText = [NSString stringWithFormat:@"My number was %i. Your number was %i. The difference is %i. \nSPLASHDOWN!\nMove forward 1 space", macPick, numberGuess, numberDiff];
}
else {
moveResultLabelText = [NSString stringWithFormat:@"My number was %i. Your number was %i. The difference is %i. \nBLACK HOLE!\nSorry. You don't get to move.", macPick, numberGuess, numberDiff];
}
[moveResultLabel setStringValue:moveResultLabelText];
}
@end
感谢这么多抽出时间来帮助菜鸟! – 2011-03-08 11:23:00