我是Objective C的新手,我需要在5次循环内完成基本的数学运算。用户需要说明将执行的操作(+, - ,*,/),程序将生成两个随机数来进行数学运算。然后用户将进行数学运算,程序将比较他的输入和正确的答案。最后,用户将根据正确或错误的答案收到分数%和自定义消息。我有点卡住了。首先,我做了一个生成两个随机数的程序并运行得很好。但是,当我添加其余代码时,我会收到一条警告消息,指出实现不完整。我也无法看到案件中的其他结构发生了什么,因为我收到的错误表明每一个都有“预期的表达”。 很感谢任何形式的帮助。 这是我的代码:
#import <Foundation/Foundation.h>
@interface myMath : NSObject
//methods
-(int) getRandomNumber;
-(void) add: (double) result;
-(void) subtract: (double) result;
-(void) multiply: (double) result;
-(void) divide: (double) result;
@end
@implementation myMath
//returns a random number between 1 and 100
-(int) getRandomNumber{
return (arc4random()%(100-1))+1;
}
@end
@interface Calculator : NSObject
@property double setaccumulator, accumulator; //Synt all methods.
@end
@implementation Calculator{
double accumulator;
}
-(void) setAccumulator: (double) value;
{ accumulator = value; }
-(void) clear
{ accumulator = 0; }
-(double) accumulator
{ return accumulator; }
-(void) add: (double) value
{ accumulator += value; }
-(void) subtract: (double) value
{ accumulator -= value; }
-(void) multiply: (double) value
{ accumulator *= value; }
-(void) divide: (double) value
{ accumulator /= value; }
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
myMath *myMathStuff;
myMathStuff = [[myMath alloc] init];
int rnum1 = [myMathStuff getRandomNumber];
int rnum2 = [myMathStuff getRandomNumber];
double result;
char operator;
Calculator *myCalculator = [[Calculator alloc]init];
int n, right, wrong, cont;
n = 0, right, wrong, cont = 0;
NSLog(@"The random numbers are %i and %i", rnum1, rnum2);
while (n <= 5){
NSLog(@"What operation do you want to perform? (+ . - . * . / .");
scanf(" %c", &operator);
[myCalculator setAccumulator:(double) rnum1];
switch (operator) {
case '+':
[myCalculator add: rnum2];
NSLog(@"Please type the addition result: ");
scanf("%lf", &result);
if (result == rnum1)
right =+1;
NSLog(@"Congratulations, you did it right!");
else
wrong =+1;
NSLog(@"Sorry, the addition result is: %.2f", [myCalculator accumulator]);
break;
case '-':
[myCalculator subtract: rnum2]
NSLog(@"Please type the subtraction: ");
scanf("%lf", &result);
if (result == rnum1)
right =+1;
NSLog(@"Congratulations, you did it right!");
else
wrong =+1,
NSLog(@"Sorry, the subtraction result is: %.2f", [myCalculator accumulator]);
break;
case '*':
[myCalculator multiply: rnum2];
NSLog(@"Please type the multiplication result: ");
scanf("%lf", &result);
if (result == rnum1)
right =+1;
NSLog(@"Congratulations, you did it right!");
else
wrong =+1,
NSLog(@"Sorry, the multiplication result is: %.2f", [myCalculator accumulator]);
break;
case '/':
[myCalculator divide: rnum2];
NSLog(@"Please type the division result: ");
scanf("%lf", &result);
if (result == rnum1)
right =+1;
NSLog(@"Congratulations, you did it right!");
else
wrong =+1,
NSLog(@"Sorry, the division result is: %.2f", [myCalculator accumulator]);
break;
default:
break;
}
++n;
}
NSLog(@"You were right %i", right);
NSLog(@"You were wrong %i", wrong);
if (right == 4)
NSLog(@"Excellent you have a perfect 100 percent score");
else if (right == 3)
NSLog(@"Good job you have a 80 percent score");
else if (right == 2)
NSLog (@"Well, you have a 60 percent score");
else if (right ==1)
NSLog(@"Sorry, you have received a 20 percent score");
}
return 0;
}
我对myMath实现做了一些更改,我也更改了“else”出现的行,如下所示。我仍然得到了预期的表达。如果有人能看到我没看到的东西,我会很感激你的意见。 我改变的部分代码: switch(operator){ 案例'+':
[myMathStuff add: rnum2];
NSLog(@"Please type the addition result: ");
scanf("%lf", &result);
if (result == rnum1)
right =+1;
NSLog (@"Congratulations, you did it right!");
else {
wrong =+1;
NSLog (@"Sorry, the addition result is: %.2f", [myMathStuff accumulator]);
}
break;
我能够找到解决方案。
答案 0 :(得分:2)
您将获得implementation is incomplete
,因为在您的MyMath
课程中,您说您在课程级别有5种方法,并且实际上只实现了其中一种方法(GetRandomNumber)。
你需要完成合同 - 即 - 编码其他四种方法将要做的事情(加,减,乘,除)。