我的代码有什么问题吗? (对象-C)

发布于 2024-11-25 06:24:43 字数 1054 浏览 0 评论 0原文

所以我现在正在尝试学习如何在 Obj-C 中编写代码,我当前的目标是编写一个简单的命令行程序来计算矩形的面积和周长;然而,当我尝试该程序时,Xcode 不断向我抛出错误。 这是我的代码:

#import <Foundation/Foundation.h>

@interface Rectangle : NSObject {
@private
    int width;
    int height;
}

-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
-(int)  width;
-(int)  height;
-(int)  area;
-(int)  perimeter;
@end

@implementation Rectangle

-(void) setWidth:(int)w
{
    width = w;
}
-(void) setHeight:(int)h
{
    height = h;
}
-(int) width
{
    return width;
}
-(int) height
{
    return height;
}
-(int) area  

正是在这一行,我收到错误“线程 1:在断点 1 处停止”

{
    return height*width;
}
-(int) perimeter
{
    return 2 * width + 2 * height;
}
@end

int main (int argc, const char * argv []){
    @autoreleasepool {
        Rectangle * shape1 = [Rectangle new];

        [shape1 setHeight: 5];
        [shape1 setWidth: 15];

        NSLog(@"the area of the rectangle is %i and the perimeter is %i", [shape1 area],    [shape1 perimeter]);
    }
    return 0;
}

So I am trying to learn how to code in Obj-C as of right now and my current goal is to write a simple command line program to calculate the area and perimeter of a rectangle; however, Xcode keeps throwing up errors at me when I try the program.
Here is my code:

#import <Foundation/Foundation.h>

@interface Rectangle : NSObject {
@private
    int width;
    int height;
}

-(void) setWidth: (int) w;
-(void) setHeight: (int) h;
-(int)  width;
-(int)  height;
-(int)  area;
-(int)  perimeter;
@end

@implementation Rectangle

-(void) setWidth:(int)w
{
    width = w;
}
-(void) setHeight:(int)h
{
    height = h;
}
-(int) width
{
    return width;
}
-(int) height
{
    return height;
}
-(int) area  

It is at this line that i receive the error " Thread 1: stopped at breakpoint 1"

{
    return height*width;
}
-(int) perimeter
{
    return 2 * width + 2 * height;
}
@end

int main (int argc, const char * argv []){
    @autoreleasepool {
        Rectangle * shape1 = [Rectangle new];

        [shape1 setHeight: 5];
        [shape1 setWidth: 15];

        NSLog(@"the area of the rectangle is %i and the perimeter is %i", [shape1 area],    [shape1 perimeter]);
    }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赤濁 2024-12-02 06:24:43

代码是正确的,在xcode中,你在左下角有一个小箭头和姿势按钮,单击它或单击右上角写有刹车点的地方,然后按播放键

the code is correct , in xcode u have a little arrow and pose button on the butom left click that or click on the top right where it is written brake point and press play

旧梦荧光笔 2024-12-02 06:24:43

你的代码是绝对正确的。这对我有用。

Your code is absolutely correct. It works for me.

不回头走下去 2024-12-02 06:24:43

单击运行按钮有一个断点按钮。有时这会让你认为代码很糟糕,但它只是停在代码中的多个地方

There was a break point button for run button that was clicked on. sometimes this make u think code bad but it just stops at multiple places in the code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文