我的代码有什么问题吗? (对象-C)
所以我现在正在尝试学习如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码是正确的,在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
你的代码是绝对正确的。这对我有用。
Your code is absolutely correct. It works for me.
单击运行按钮有一个断点按钮。有时这会让你认为代码很糟糕,但它只是停在代码中的多个地方
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