我是新的目標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實施一些變化,我也改變了線路,其中「其他」顯示爲我下面展示。我仍然得到預期的表達。如果其他人可以看到我沒有看到的東西,我會感激你的意見。 開關(操作者){ 情況下 '+':我改變了代碼的 部分
[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;
我能夠找到解決方案。
謝謝Jay!在你指出這一點之後,我能夠做一些改變,並且我不再有這個警告。但是,我仍然在其他代碼行上獲得預期的表達式。在休息之前,我確實添加了{之後和}。但我仍然得到同樣的失敗警告。我把它們拿出來,程序運行。沒有我想要的方式。因爲結果是巨大的,我需要與計數器正確和錯誤工作。 再次感謝。 – stdntmp
沒問題,如果此響應幫助您,請通過選中對勾將其標記爲答案。 –
它確實有幫助。不過,我仍然有一些我發佈的初始問題。 – stdntmp