2013-09-23 46 views
0

我正在製作一個簡單的關於形狀的命令行應用程序。我已經創建了變量的顏色和數量,並在if語句中使它們成爲可設置的屬性。我需要能夠獲得形狀的名稱,但不能將其設置爲變量。所以這個類需要計算並返回名稱,而不是一個可設置的屬性。計算變量的類

能否請您告訴我做這件事的最佳方法,因爲我不確定。我試圖將if語句放在實現文件中,但XCode不喜歡它。

任何建議都會被佔用。

類的頭文件:

@interface shape : NSObject 

@property (nonatomic, strong) NSString *numberOfSides; 
@property (nonatomic, strong) NSString *name; 
@property int *sides; 

@property (nonatomic, strong) NSString *colour; 

void waitOnCR (void); 

Main.m 
#import <Foundation/Foundation.h> 
#import "shape.h" 

int main(int argc, const char * argv[]) 
{ 

    @autoreleasepool { 

     //Array of colours thats index is randomly selected from when the program is run 
     NSArray *colours = [NSArray arrayWithObjects:@"Red",@"Blue",@"Green", @"Yellos", @"Ornage", @"Purple", @"Pink", nil]; 
     NSUInteger random = arc4random() % [colours count]; 

     NSLog(@"Enter a number from between 3-8"); 

       float user; 

     scanf("%f" , &user); 

     if (user == 3) 
     { 
      shape *myShape = [[shape alloc]init]; 
      [myShape setSides:@3]; 
      [myShape setColour:@"Red"]; 

      NSLog(@"The %@ shape has %@ and is called a %@", [myShape colour], [myShape sides], [myShape name]); 
     } 
     else if (user == 4) 
     { 
      shape *myShape = [[shape alloc]init]; 
      [myShape setSides:@4]; 
      [myShape setColour:@"Blue"]; 

      NSLog(@"The %@ shape has %@ and is called a %@", [myShape colour], [myShape sides], @"Square"); 
     } 
     else if (user == 5) 
     { 
      shape *myShape = [[shape alloc]init]; 
      [myShape setName:@"Pentagon"]; 
      [myShape setNumberOfSides:@"5"]; 

      NSLog(@"The %@ shape has %@ and is called a %@", [colours objectAtIndex:random], [myShape numberOfSides], [myShape name]); 

     } 
     else if (user == 6) 
     { 
      shape *myShape = [[shape alloc]init]; 
      [myShape setName:@"Hexagon"]; 
      [myShape setNumberOfSides:@"6"]; 

      NSLog(@"The %@ shape has %@ and is called a %@", [colours objectAtIndex:random], [myShape numberOfSides], [myShape name]); 

     } 
     else if (user == 7) 
     { 
      shape *myShape = [[shape alloc]init]; 
      [myShape setName:@"Heptagon"]; 
      [myShape setNumberOfSides:@"7"]; 

      NSLog(@"The %@ shape has %@ and is called a %@", [colours objectAtIndex:random], [myShape numberOfSides], [myShape name]); 
     } 
     else if (user == 8) 
     { 
      shape *myShape = [[shape alloc]init]; 
      [myShape setName:@"Octagon"]; 
      [myShape setNumberOfSides:@"8"]; 

      NSLog(@"The %@ shape has %@ and is called a %@", [colours objectAtIndex:random], [myShape numberOfSides], [myShape name]); 
    } 
+0

大家都應該看看我們的電子結構球更好的做法,或者你寧願提供代碼? – ppeterka

+0

你能否請在這裏發佈錯誤代碼片段? – nio

+0

請問你能解釋一下問題所在。你不能修改Shape類或什麼? – PSsam

回答

0

如果你需要計算的名字,你可以寫這樣的事情。 形狀+ CalculableName.h

#import "Shape.h" 

@interface Shape (CalculableName) 

@property (nonatomic, strong) NSString *calculatedName; 

@end 

形狀+ CalculableName.m

#import "Shape+CalculableName.h" 

@implementation Shape (CalculableName) 

- (NSString *)calculatedName { 
    if (self.name) { 
     return self.name; 
    } 
    return [NSString stringWithFormat:@"%@ %@", self.colour, self.numberOfSides ]; 
} 

@end 

而且offtopic,開關操作的使用比使用IFS