2013-05-16 174 views
-5

我試圖在考試結束時向用戶顯示百分比。基本上,我需要一行代碼,將myScore除以70的問題總數。任何幫助都將非常感謝。在目標C中顯示百分比

這是我的考試結束碼:

NSInteger endOfQuiz = [theQuiz count]; 
if((((questionNumber - 1) * 6) + 6) == endOfQuiz) 
{ 
    // Game is over. 
    if(myScore >= 49) 
    { 
     NSString *finishingStatement = [[NSString alloc] initWithFormat:@"Practice Exam concluded. Congratulations, you have passed with a 70%% or better! \nYou scored %i out of 70!", myScore]; 
     theQuestion.text = finishingStatement; 
     answerResult.text = @""; 
     counterLabel.text = @"";  } 
    else 
    { 
     NSString *finishingStatement = [[NSString alloc] initWithFormat:@"Practice Exam concluded. Unfortunately, you have failed with less than 70%%, keep practicing! \nYou scored %i out of 70.", myScore]; 
     theQuestion.text = finishingStatement; 
     counterLabel.text = @""; 

    } 
    theLives.text = @""; 
    answerResult.text = @""; 
    // Make button 1 appear as a reset game button 
    restartGame = YES; 
    [answerOne setHidden:NO]; 
    [answerOne setTitle:@" Try this Practice Exam again" forState:UIControlStateNormal]; 

} 
+1

那麼,什麼是錯'浮動比例= 100×(浮動)將myScore/70;「(你乘以100得到的百分比。) – Caleb

回答

0

除法運算符在C(以及因此的Objective-C)爲/

myScore * 100/70 
+0

看起來'myScore'可能是一個整數,所以你的表現?也會產生一個int,嘗試將'myScore'轉換爲'float'或者簡單地將'70'改爲'70.0' – Caleb

+1

即使它不是浮點數,它也會工作,因爲我們先乘以100,除以70會產生百分比的整數部分 – Brigham

+0

謝謝!我知道有一個簡單的答案,而迦勒,你是對的,讚賞它們。 – user2175392