我会为新的可可程序员提供帮助

时间:2011-03-08 06:00:20

标签: cocoa objective-c-2.0

我正在为设计游戏的女儿制作一个简单的程序。我只是希望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

2 个答案:

答案 0 :(得分:1)

if语句中,您有赋值运算符=而不是比较运算符==,因此每个if都为真,最后执行的是{1} 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);

答案 1 :(得分:0)

嘿,我花了一分钟才弄明白发生了什么。

简单地说,之间存在差异:

if (numberDiff=1) {

if (numberDiff==1) {

第一个是分配。它将取值1,将其分配到numberDiff,并使用numberDiff的新值作为if语句的条件。由于1是真值,因此会成功。

第二个是进行比较。它将取值1并将其与numberDiff的值进行比较。如果两个值相等,那么整个语句将产生一个真值。

基本上,您需要使用==而不是=

你总是看到“3”案例的结果,因为那是最后一个if语句。该计划可以将3分配到numberDiff,因此它将使用该案例中的文字更新您的NSTextField