如果及其在Kotlin中的语句

发布于 2025-01-18 11:30:10 字数 751 浏览 2 评论 0原文

好的,我对的逻辑有一个问题,而语句我不了解逻辑不起作用。我在其他编程中使用了该声明,但这是我想做的。

我正在使用一个按钮,其中包括我的SQRT功能。在按钮中,我试图使用逃生路线。当我按下按钮时,它会产生平方根。但是,当我再次按下按钮时,它会崩溃。

因此,为了临时修复和学习目的,我已经完成了此操作:

 binding.btnSqrt.setOnClickListener{
    val df = DecimalFormat("#.####")
    if(!tv_equation.text.isNullOrEmpty())
    {
        val number = tv_equation.text.toString().toDouble()
        binding.tvResult.text = df.format(sqrt(number))
    else
      { 
       binding.tvResult.text = ""
      binding.tvEquation.append("")
}

但是,如果else语句,它并不意味着。我正在研究该语句在WPF和UWP中的工作方式。

当我再次按下按钮之后,它只会使答案加倍。例如,在TVResult中,如果我有4个,则在再次按下按钮时应该清除,但是它的作用是双重的,并添加了曾经的内容,因此它显示了44,而不是清除文本视图和EDITTEXT。

我是否在Kotlin中使用了错误的语句。还是按钮最初编码的方式?

Ok I have an issue with the logic of the if and else statement I am not understanding why and how the logic doesn't work. I have used the statement in other programming but this is what I am wanting to do.

I am using a button with my sqrt function. In the button I am trying to use an escape route. When I press the button it gives the square root. However when I press the button again it crashes.

So for a temporary fix and learning purpose I have done this:

 binding.btnSqrt.setOnClickListener{
    val df = DecimalFormat("#.####")
    if(!tv_equation.text.isNullOrEmpty())
    {
        val number = tv_equation.text.toString().toDouble()
        binding.tvResult.text = df.format(sqrt(number))
    else
      { 
       binding.tvResult.text = ""
      binding.tvEquation.append("")
}

However it does not imply the if else statement. I am looking at this how the statement works in WPF and UWP.

When I press the button again after that it just doubles the answer. For example in tvResult if I have 4 then when the button is pressed again it should clear but what it does is double and adds what ever is there so it shows 44 instead of clearing the textview and edittext.

Am I using the if and else statement wrong in kotlin. Or is it just the way the button is originally coded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文