2011-03-08 73 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