You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开发Kotlin猜数字游戏时,无输入点击按钮程序崩溃求助

猜数字游戏空输入崩溃问题修复

问题根源

  • 崩溃原因:当用户未输入任何内容就点击按钮时,userAnsText是空字符串,直接调用toInt()会抛出NumberFormatException,直接导致程序崩溃。
  • 初始if条件失效:userAnsInt是通过toInt()生成的非空Int类型,永远不可能等于null,所以if(userAnsInt == null)这个判断逻辑永远不会触发。

修复方案

使用toIntOrNull()进行安全类型转换——当输入为空或不是有效数字时,这个方法会返回null,同时调整条件判断的顺序:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val button = findViewById<Button>(R.id.guessBtn)
        button.setOnClickListener {
            buttonPressed()
        }
    }

    private fun buttonPressed() {
        val result = findViewById<TextView>(R.id.result)
        val userAnsEditText = findViewById<EditText>(R.id.answer)
        val userAnsText = userAnsEditText.text.toString().trim() // 去除首尾空白字符,避免空格触发空提示
        val userAnsInt = userAnsText.toIntOrNull() // 安全转换,失败返回null

        val answerCorrect = "Congrats, you have correctly guessed the number"
        val answerWrong = "Sorry,you have guessed the wrong number"
        val answerEmpty = "Please enter a number"

        val randomValues = Random.nextInt(1, 11)

        // 优先判断输入是否有效
        if(userAnsInt == null) {
            result.text = answerEmpty
        }
        else if(userAnsInt == randomValues) {
            result.text = answerCorrect
        }
        else {
            result.text = answerWrong
        }
    }
}

额外优化点

把视图查找逻辑移到onCreate中,避免每次点击按钮都重复查找视图,提升性能:

class MainActivity : AppCompatActivity() {
    // 提前声明视图变量
    private lateinit var resultTextView: TextView
    private lateinit var userAnsEditText: EditText
    private lateinit var guessButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        // 仅初始化时查找一次视图
        resultTextView = findViewById(R.id.result)
        userAnsEditText = findViewById(R.id.answer)
        guessButton = findViewById(R.id.guessBtn)
        
        guessButton.setOnClickListener {
            buttonPressed()
        }
    }

    private fun buttonPressed() {
        val userAnsText = userAnsEditText.text.toString().trim()
        val userAnsInt = userAnsText.toIntOrNull()

        val answerCorrect = "Congrats, you have correctly guessed the number"
        val answerWrong = "Sorry,you have guessed the wrong number"
        val answerEmpty = "Please enter a number"

        val randomValues = Random.nextInt(1, 11)

        if(userAnsInt == null) {
            resultTextView.text = answerEmpty
        }
        else if(userAnsInt == randomValues) {
            resultTextView.text = answerCorrect
        }
        else {
            resultTextView.text = answerWrong
        }
    }
}

内容的提问来源于stack exchange,提问作者Blinky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 19:16:18