在我的添加功能 answerLabel.text 中,它给了我一个错误。它说'String'不能转换为'Int'而我正试图从secondStep2获得我所得到的并输入我的函数参数
//Adding Function
changingSignsLabel.text = "+"
let firstDenomInTextField:Double! = (firstDenominatorTextField.text as NSString).doubleValue
let firstNumInTextField:Double! = (firstNumeratorTextField.text as NSString).doubleValue
let firstWholeInTextField:Double! = (firstWholeNumberTextField.text as NSString).doubleValue
let secondDenomInTextField:Double! = (secondDenominatorTextField.text as NSString).doubleValue
let secondNumInTextField:Double! = (secondNumeratorTextField.text as NSString).doubleValue
let secondWholeInTextField:Double! = (secondWholeNumberTextField.text as NSString).doubleValue
var firstStep = firstDenomInTextField! * firstWholeInTextField! / firstDenomInTextField!
var secondStep = firstStep + firstNumInTextField! / firstDenomInTextField!
var thirdStep = secondDenomInTextField! * secondWholeInTextField! / secondDenomInTextField!
var fourthStep = thirdStep + secondNumInTextField! / secondDenomInTextField!
var calculatedAnswer = (secondStep + fourthStep)
answerLabel.hidden = false
var firstStep2 = calculatedAnswer / 1
var secondStep2 = "\(firstStep2 * 10 * 10)"
answerLabel.text = printSimplifiedFraction(Numerator: secondStep2)
我的功能
func printSimplifiedFraction(Numerator numerator: Int, Denominator denominator: Int = 100)
{
var finalNumerator = numerator;
var finalDenominator = denominator;
var wholeNumbers:Int = numerator / denominator;
var remainder:Int = numerator % denominator;
//println("wholeNumbers = \(wholeNumbers), remainder = \(remainder)");
//println("\(denominator) % \(remainder) = \(denominator % remainder)");
if(remainder > 0)
{
// see if we can simply the fraction part as well
if(denominator % remainder == 0) // no remainder means remainder can be simplified further
{
finalDenominator = denominator / remainder;
finalNumerator = remainder / remainder;
}
else
{
finalNumerator = remainder;
finalDenominator = denominator;
}
}
if(wholeNumbers > 0 && remainder > 0)
{
// prints out whole number and fraction parts
println("Simplified fraction of \(numerator)/\(denominator) = \(wholeNumbers) \(finalNumerator)/\(finalDenominator)");
}
else if (wholeNumbers > 0 && remainder == 0)
{
// prints out whole number only
println("Simplified fraction of \(numerator)/\(denominator) = \(wholeNumbers)");
}
else
{
// prints out fraction part only
println("Simplified fraction of \(numerator)/\(denominator) = \(finalNumerator)/\(finalDenominator)");
}
}
我的问题如何让函数接受我的变量?
答案 0 :(得分:2)
我相信这就是你想要的,首先在你的添加功能中应该是:
var firstStep2 = calculatedAnswer / 1
var secondStep2 = Int(firstStep2 * 10 * 10)
answerLabel.text = printSimplifiedFraction(Numerator: secondStep2)
然后你的print ...方法应该像这样改变(注意它返回一个字符串):
func printSimplifiedFraction(Numerator numerator: Int, Denominator denominator: Int = 100) -> String
{
// I haven't looked into this bit
...
if(wholeNumbers > 0 && remainder > 0)
{
return ("Simplified fraction of \(numerator)/\(denominator) = \(wholeNumbers) \(finalNumerator)/\(finalDenominator)")
}
else if (wholeNumbers > 0 && remainder == 0)
{
return ("Simplified fraction of \(numerator)/\(denominator) = \(wholeNumbers)")
}
else
{
return ("Simplified fraction of \(numerator)/\(denominator) = \(finalNumerator)/\(finalDenominator)")
}
}
另外,查看代码我认为你可以简化逻辑(我还在评论中按照你的意思转换为整数):
let firstDenomInTextField = firstDenominatorTextField.text.toInt()
let firstNumInTextField = firstNumeratorTextField.text.toInt()
let firstWholeInTextField = firstWholeNumberTextField.text.toInt()
let secondDenomInTextField = secondDenominatorTextField.text.toInt()
let secondNumInTextField = secondNumeratorTextField.text.toInt()
let secondWholeInTextField = secondWholeNumberTextField.text.toInt()
var firstStep = firstWholeInTextField! + firstNumInTextField! / firstDenomInTextField!
var secondStep = secondWholeInTextField! + secondNumInTextField! / secondDenomInTextField!
var calculatedAnswer = (firstStep + secondStep)
var numerator = Int(calculatedAnswer * 10 * 10)
answerLabel.text = printSimplifiedFraction(Numerator: numerator)
答案 1 :(得分:0)
试试这个:
var str:String = "abc"
var a:Int? = str.toInt()
if (a != nil) {
printSimplifiedFraction(Numerator: str!)
}
并为函数添加返回值
func printSimplifiedFraction(Numerator numerator: Int, Denominator denominator: Int = 100) ->String {
var finalNumerator = numerator;
var finalDenominator = denominator;
var wholeNumbers:Int = numerator / denominator;
var remainder:Int = numerator % denominator;
if(remainder > 0)
{
// see if we can simply the fraction part as well
if(denominator % remainder == 0) // no remainder means remainder can be simplified further
{
finalDenominator = denominator / remainder;
finalNumerator = remainder / remainder;
}
else
{
finalNumerator = remainder;
finalDenominator = denominator;
}
}
var result:String = "Simplified fraction of \(numerator)/\(denominator) = ";
if(wholeNumbers > 0 && remainder > 0)
{
// prints out whole number and fraction parts
result += "\(wholeNumbers) \(finalNumerator)/\(finalDenominator)"
}
else if (wholeNumbers > 0 && remainder == 0)
{
// prints out whole number only
result += "\(wholeNumbers)"
}
else
{
// prints out fraction part only
result += "\(finalNumerator)/\(finalDenominator)"
}
println("\(result)")
return result
}
答案 2 :(得分:0)
如果要分配从函数返回的值,请将返回类型添加到函数并返回值。
func printSimplifiedFraction(Numerator numerator: Int, Denominator denominator: Int = 100) -> String {
//add end of your function
return yourValue;
}
在您的代码中确实如此
var secondStep2 = (firstStep2 * 10 * 10)
answerLabel.text = printSimplifiedFraction(Numerator: secondStep2)
func printSimplifiedFraction(Numerator numerator: Int, Denominator denominator: Int = 100) -> String
{
var finalNumerator = numerator;
var finalDenominator = denominator;
var wholeNumbers:Int = numerator / denominator;
var remainder:Int = numerator % denominator;
//println("wholeNumbers = \(wholeNumbers), remainder = \(remainder)");
//println("\(denominator) % \(remainder) = \(denominator % remainder)");
if(remainder > 0)
{
// see if we can simply the fraction part as well
if(denominator % remainder == 0) // no remainder means remainder can be simplified further
{
finalDenominator = denominator / remainder;
finalNumerator = remainder / remainder;
}
else
{
finalNumerator = remainder;
finalDenominator = denominator;
}
}
var returnValue :String
if(wholeNumbers > 0 && remainder > 0)
{
returnValue = "Simplified fraction of \(numerator)/\(denominator) = \(wholeNumbers) \(finalNumerator)/\(finalDenominator)"
}
else if (wholeNumbers > 0 && remainder == 0)
{
returnValue = "Simplified fraction of \(numerator)/\(denominator) = \(wholeNumbers)"
}
else
{
returnValue = "Simplified fraction of \(numerator)/\(denominator) = \(finalNumerator)/\(finalDenominator)"
}
return returnValue
}