我正在爲我的學生製作評分應用程序,但我的比較運算符未按照我期望的那樣運行。我的代碼如下;比較運算符無法正常工作Xcode
float FINAL = ((_gradeyouwant - (_quarter1 * 0.2f) - (_quarter2 * 0.2f) - (_quarter3 * 0.2f) - (_quarter4 * 0.2f) - (_quarterM * 0.1f))/0.1f);
NSLog(@"q1 = %.2f", _quarter1);
NSLog(@"q2 = %.2f", _quarter2);
NSLog(@"q3 = %.2f", _quarter3);
NSLog(@"q4 = %.2f", _quarter4);
NSLog(@"qM = %.2f", _quarterM);
NSLog(@"qF = %.2f", FINAL);
NSLog(@"grade = %.2f", _gradeyouwant);
if ((FINAL > 4.3f))
{
[_result setText:[NSString stringWithFormat:@"It is not possible to get your desired grade."]];
}else if ((FINAL > 4.0f))
{
[_result setText:[NSString stringWithFormat:@"You would need to get an A+"]];
}else if ((FINAL > 3.7f))
{
[_result setText:[NSString stringWithFormat:@"You would need to get an A"]];
}else if ((FINAL > 3.3f))
ETC. ETC.
當你用NSLog查看輸出時,它會告訴我所有事情的正確值。但是,如果我使FINAL爲4.0,它不打印正確的字符串。我認爲,當它達到FINAL> 4.0時,它不會運行該線。但它確實如此。我究竟做錯了什麼?非常感謝!
我對分級系統並不熟悉,但您的比較(FINAL>?)爲1位小數,所以我會建議在執行比較之前舍入FINAL。 –
@RoboticCat,感謝您的輸入。我會嘗試。雖然得到4.0程序將0.4分爲0.1。因此,它的價值應該是4.000像許多零的感覺。謝謝!! – Douglas