提问人:Blinky 提问时间:11/10/2023 最后编辑:RobotnikBlinky 更新时间:11/15/2023 访问量:39
未输入输入并按下按钮时程序崩溃
Program crashes when no input is entered and button is pressed
问:
我正在制作一个随机数猜测游戏。问题是,如果用户没有输入任何数字,而是在if条件下按下按钮,则应执行answerEmpty变量,并且结果文本中应显示“请输入数字”。
但是当我按下按钮时,程序崩溃了。else if 和 else 条件有效,但起始 if 条件不起作用。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Calling button method
val button = findViewById<Button>(R.id.guessBtn)
//Set on click listener for the button
button.setOnClickListener {
buttonPressed()
}
}
private fun buttonPressed() {
//Method to find view from activity_main
val result = findViewById<TextView>(R.id.result)
val userAnsEditText = findViewById<EditText>(R.id.answer)
val userAnsText = userAnsEditText.text.toString()
val userAnsInt = userAnsText.toInt()
//This variable will display the answer
val answerCorrect = "Congrats, you have correctly guessed the number"
val answerWrong = "Sorry,you have guessed the wrong number"
val answerEmpty = "Please enter a number"
//Making random numbers from 1 to 10
val randomValues = Random.nextInt(1, 11)
//Adding a condition to check if user answer matches the random answer
if(userAnsInt == null) {
result.text = answerEmpty
}
else if(userAnsInt == randomValues) {
result.text = answerCorrect
}
else {
result.text = answerWrong
}
}
}
答:
0赞
Muhammad Kamran Saeed
11/10/2023
#1
使用 Try catch 块, 例如
`
try {
// Code that may cause an exception
int result = 10 / 0; // This will throw an ArithmeticException
Log.d("MyApp", "Result: " + result); // This line won't be executed
} catch (ArithmeticException e) {
// Handle the exception
Log.e("MyApp", "An arithmetic exception occurred: " + e.getMessage());
}
` 以下是各部分的细分:
尝试:可能引发异常的代码放置在此块中。抓住:如果 try 块中发生异常,则执行 catch 块。它指定要捕获的异常类型以及要处理该异常的代码。最后:此块包含无论是否发生异常都将执行的代码。 这是可选的。 之后,您的应用程序将不会崩溃。它将显示错误,您可以在 Toast 或输出窗口中打印错误。
评论
0赞
Blinky
11/10/2023
对不起,我正在使用 Kotlin 和初学者.你能把代码翻译成 Kotlin 吗?
0赞
Harshali
11/10/2023
#2
在解析到 in 之前,您应该检查文本是否为空条件EditText
int
添加以下条件
if (userAnsText.isEmpty()) {
result.text = answerEmpty
}
行前。val userAnsInt = userAnsText.toInt()
它将修复您的崩溃。
添加条件后,您的函数将在下面显示。buttonPressed()
isEmpty()
private fun buttonPressed() {
//Method to find view from activity_main
val result = findViewById<TextView>(R.id.result)
val userAnsEditText = findViewById<EditText>(R.id.answer)
val userAnsText = userAnsEditText.text.toString()
val answerCorrect = "Congrats, you have correctly guessed the number"
val answerWrong = "Sorry,you have guessed the wrong number"
val answerEmpty = "Please enter a number"
if (userAnsText.isEmpty()) {
result.text = answerEmpty
} else {
val userAnsInt = userAnsText.toInt()
//This variable will display the answer
//Making random numbers from 1 to 10
val randomValues = Random.nextInt(1, 11)
//Adding a condition to check if user answer matches the random answer
//when condition more relavant in kotlin than if-else if-else
when (userAnsInt) {
null -> result.text = answerEmpty
randomValues -> result.text = answerCorrect
else -> result.text = answerWrong
}
}
}
评论