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]);
    }
4

1 に答える 1

0

名前を計算する必要がある場合は、次のように記述できます。Shape+CalculableName.h

#import "Shape.h"

@interface Shape (CalculableName)

@property (nonatomic, strong) NSString *calculatedName;

@end

形状+計算可能な名前.m

#import "Shape+CalculableName.h"

@implementation Shape (CalculableName)

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

@end

そしてオフトピック、スイッチ演算子の使用は、ifs を使用するよりも優れた方法です

于 2013-09-23T13:50:40.107 に答える