I have google this and searched here but I have hit a wall.
I have a function which is to return an Int. It tells me I need to add another return expression to the overall function but I can't figure out where it needs to go to be done correctly, i have tried a few variations including adding the same return I use within the if statements but before the closing tag which throws up errors within the rest of the code
I call the function like this;
var ans = GetQuestion(1)
fun GetQuestion(cat:Int): Int {
val btn1 = findViewById(R.id.button1) as Button
val btn2 = findViewById(R.id.button2) as Button
val btn3 = findViewById(R.id.button3) as Button
val btn4 = findViewById(R.id.button4) as Button
btn1.setVisibility(View.VISIBLE)
btn2.setVisibility(View.VISIBLE)
btn3.setVisibility(View.VISIBLE)
btn4.setVisibility(View.VISIBLE)
val initialimageview = findViewById(R.id.imageview_Qresponse) as ImageView
initialimageview.setBackgroundResource(R.drawable.question_2);
val viewQ = findViewById(R.id.textview_question) as TextView
if (cat==1) { // Multiplication
var a = rand(5,80)
var b = rand(2,50)
val ans = a * b
var opType = " X "
viewQ.text = ("" + a + " " + opType + " " + b + "?");
val listOfAns = mutableListOf(ans,ans-a,ans-b,ans+a)
listOfAns.shuffle()
btn1.text=("" + listOfAns.elementAt(0))
btn2.text=("" + listOfAns.elementAt(1))
btn3.text=("" + listOfAns.elementAt(2))
btn4.text=("" + listOfAns.elementAt(3))
return ans
listOfAns.clear()
}
if (cat==2) { // Addition
var a = rand(5,250)
var b = rand(2,140)
val ans = a + b
var opType = " + "
viewQ.text = ("" + a + " " + opType + " " + b + "?");
val listOfAns = mutableListOf(ans,ans+rand(2,4),ans+rand(5,10),ans-rand(2,10))
listOfAns.shuffle()
btn1.text=("" + listOfAns.elementAt(0))
btn2.text=("" + listOfAns.elementAt(1))
btn3.text=("" + listOfAns.elementAt(2))
btn4.text=("" + listOfAns.elementAt(3))
return ans
listOfAns.clear()
}
if (cat==3) { // Subtraction
var a = rand(80,150)
var b = rand(2,79)
var ans = a - b
var opType = " - "
viewQ.text = ("" + a + " " + opType + " " + b + "?");
val listOfAns = mutableListOf(ans,ans+rand(2,5),ans+rand(6,10),ans-rand(2,10))
listOfAns.shuffle()
btn1.text=("" + listOfAns.elementAt(0))
btn2.text=("" + listOfAns.elementAt(1))
btn3.text=("" + listOfAns.elementAt(2))
btn4.text=("" + listOfAns.elementAt(3))
return ans
listOfAns.clear()
}
if (cat==4) { // Division
var safedivision = rand(3,16)
var b = rand(2,20)
var a = b * safedivision
var ans = a / b
var opType = " / "
viewQ.text = ("" + a + " " + opType + " " + b + "?");
val listOfAns = mutableListOf(ans,ans+rand(2,5),ans+rand(6,10),ans-rand(2,10))
listOfAns.shuffle()
btn1.text=("" + listOfAns.elementAt(0))
btn2.text=("" + listOfAns.elementAt(1))
btn3.text=("" + listOfAns.elementAt(2))
btn4.text=("" + listOfAns.elementAt(3))
return ans
listOfAns.clear()
}
}
As a side note I wasn't sure if I have to use var of val when writing var ans = GetQuestion(1) however both methods throw up the same return error so I can amend that later if required