2011-03-08 22 views
1

我正在为设计游戏的女儿做一个简单的程序。我只想让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 

回答

0

嘿,我花了一分钟才弄清楚发生了什么事。

简单地说,有之间的差异:

if (numberDiff=1) { 

而且

if (numberDiff==1) { 

第一个是做一个分配。它将采用值1,将其分配到numberDiff,并使用新值numberDiff作为if语句的条件。由于1是一个真正的价值,这将成功。

第二个是做比较。它将采用值1并将其与numberDiff的值进行比较。如果这两个值相等,那么总体说明将产生一个真值。

所以基本上,你需要使用==而不是=

您总是看到“3”情况的结果,因为这是最后一条if语句。该程序能够将3分配到numberDiff,因此它将使用该案例中的文本更新NSTextField

+0

感谢这么多抽出时间来帮助菜鸟! – 2011-03-08 11:23:00

1

在你if语句必须赋值运算符=,而不是比较操作==,所以每if是真实的,执行的最后一个是一个3

你会更好使用这个switch

switch(numberDiff) 
{ case 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]; 
     break; 
    case 1: 
     etc. 
    default: 
     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]; 
} 

接下来,您必须声明所有的变量为全局文件的顶部:

int macPick; 
int numberGuess; 
int numberDiff; 

如果这些不同的共享方法,那么它们应该是实例变量,在这种情况下,macPick是一个实例变量。这应该在@interface声明:

@interface AstroGuessAppDelegate : NSObject 
{ 
    int macPick; 
    etc. 

它看起来像另外两个只用于compareNumbersAndSendResults如此宣布他们有:

- (IBAction)compareNumbersAndSendResults:(id)sender 
{ 
    int numberGuess; 
    int numberDiff; 
    etc. 

在一个小的变化为:

numberDiff = numberDiff * -1; 

numberDiff = -numberDiff; 

甚至使用ABS功能:

numberDiff = abs(macPick - numberGuess); 
+0

非常感谢。我新是愚蠢的。我甚至在我的工作,通过对破坏“=” VS在书上读到“==”可能会导致。我知道这是一个愚蠢的事情,很基本的东西,我真的很感谢您抽出宝贵的时间来帮助一支新秀。我原本的ABS功能,但试图在世界上的一切改变我的代码,看看我是否可以修复它。哈。 '案例'是非常有用的。再次,这是我看,但我在比赛中很早就没想到它。 – 2011-03-08 11:22:20

相关问题