我正在編寫一個程序,用於計算用戶輸入的簡單數字。 2,4,7,10,12等等。存儲用戶輸入,計算並顯示到底
我需要程序以某種方式存儲這些輸入(來自不同文本框)。在內部公式中使用它們,最後將它們展示給用戶。
我該怎麼做?請注意,我在編程方面頗爲新穎。
我知道如何定義textFields,做一個計算IBAction,並且我已經嘗試創建一個 - (void)calculate,它可以工作,但只能在1個輸入上。當我執行下面的代碼,輸入6個文本字段而不是3個(用在第一行(resultOne))時,它給了我錯誤的數字。但我仍然需要一個更好的方式來做到這一點。
-(void) calculate{
weightOne = [v1Text.text floatValue]/2;
weightTwo = [v2Text.text floatValue]/2;
resultOne = [m1Text.text floatValue] * weightOne + [s1Text.text floatValue] * weightOne;
resultTwo = [m2Text.text floatValue] * weightTwo + [s2Text.text floatValue] * weightTwo;
_resultTotal = (resultOne + resultTwo)/(weightOne + weightTwo);
NSString *resultString = [[NSString alloc]
initWithFormat: @"%.2f", resultOne];
resultLabel.text = resultString;
[resultString release];
}
我知道上面的代碼輸出resultOne,但這只是爲了我自己的測試,要知道方程的工作原理。但我真的希望全新開始,因爲我不相信上面的代碼適合我需要它做的事情。
請記住,我是一名初學者,並多解釋一點,而不僅僅是向我展示一些代碼。
我期待着你能給我的任何幫助。
編輯! - 下面是新的代碼
-(IBAction) calculate:(id)sender {
weightOne = 1;
weightTwo = 0.75;
weightThree = 0.50;
mOne = [m1Text.text floatValue];
mTwo = [m2Text.text floatValue];
sOne = [s1Text.text floatValue];
sTwo = [s2Text.text floatValue];
resultOne = (mOne*weightOne) + (sOne*weightOne);
resultTwo = (mTwo*weightTwo) + (sTwo*weightTwo);
_resultTotal = (resultOne + resultTwo)/(weightOne*2 + weightTwo*2);
NSString *resultString = [[NSString alloc]
initWithFormat: @"Gennemsnit %.2f", _resultTotal];
resultLabel.text = resultString;
}
我決定改變我的代碼,以方便地計算出輸入。現在我有另一個問題。
是否有可能以某種方式保存此公式,所以我可以重複使用它多次,而無需一次又一次鍵入代碼。
此代碼看起來不錯,我不知道爲什麼你不滿意它!當你有更多的投入時,有什麼問題? – jrturton
問題是我不知道我如何能夠存儲用戶輸入,供以後使用。用戶需要在15x2文本框中輸入最多30個等級,具體取決於他們所採用的課程數量。然後,我需要以簡單的方式計算分數,並最終顯示給用戶。我不確定我到目前爲止的代碼是如何做到的。 –