gpt4 book ai didi

android - Kotlin 返回表达式

转载 作者:行者123 更新时间:2023-12-05 00:19:03 24 4
gpt4 key购买 nike

我用谷歌搜索并在这里搜索,但我碰壁了。
我有一个返回 Int 的函数。它告诉我我需要向整个函数添加另一个返回表达式,但我无法弄清楚它需要在哪里正确完成,我尝试了一些变体,包括添加我在 if 语句中使用但之前使用的相同返回在其余代码中引发错误的结束标记
我这样调用函数;

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()
}
}
附带说明一下,我不确定在编写 var ans = GetQuestion(1) 时是否必须使用 var of val 但是这两种方法都会引发相同的返回错误,因此我可以稍后根据需要进行修改

最佳答案

您可以保留变量 回答 在函数的顶部如下 -

 var ans = 0
并替换 回复 来自 的所有 if 条件回答 .在最后 之前}
添加返回语句
return ans

关于android - Kotlin 返回表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64695906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com