循環很好地工作,直到輸入退出條件,而不是在進入12345後退出,循環中的下一條語句執行。我試過把第一個語句放在循環之外,只把計算放在循環中。一旦滿足輸入條件,目標C while循環就不會退出
它只是忽略了條件。
這是代碼
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
int firstValue, pointsReceived;
while(firstValue != 12345){
NSLog(@"Enter the total points available (enter 12345 to stop program):\n");
scanf("%i", &firstValue);
NSLog(@"Enter the total points received: \n");
scanf("%i", &pointsReceived);
float percentage = (100 * pointsReceived)/firstValue;
NSLog(@"Your total percentage is: %.2f%%", percentage);
if(percentage >= 90){
NSLog(@"Your grade is an A");
}
else if(percentage >= 80){
NSLog(@"Your grade is a B");
}
else if(percentage >= 70){
NSLog(@"Your grade is a C");
}
else if(percentage >= 60){
NSLog(@"Your grade is a D");
}
else{
NSLog(@"Your grade is an F");
}
}
NSLog(@"Done!");
}
return 0;
}
後寫的,但我沒有看到你設置firstValue價值的循環中。 – 2014-09-29 16:55:36
如果你希望它立即停止,只需在firstValue的scanf後面插入一個if,如果它等於12345,就打開它。 – billpcs 2014-09-29 17:06:47