我有一个程序,它接受一个整数作为用户的输入(使用系统调用 5)。当用户输入特殊字符时!@ # $ % ^ _ - ) (而不是整数它崩溃,我怎样才能修复程序以显示错误消息而不是崩溃?
这是我写的代码
la $a0, prompt # prompt the message ask for the answer n
li $v0, 4
syscall
li $v0, 5 # input the answer
syscall
add $t1, $zero, $v0
# store the answer in $t1
blt $t1, $s0, negative # check for number entered < $s0
bgt $t1, $s1, exceed # check for > $s1 number
la $a0, blank # blank line
li $v0, 4
syscall
样本输出:
This is a number guessing game between 0- 100. Let's begin.
Chances remains: 6
Enter your guess: 56
The secret number is higher than : 56
Chances remains: 5
Enter your guess: 67
The secret number is higher than : 67
Chances remains: 4
Enter your guess: 75
The secret number is lower than : 75
Chances remains: 3
Enter your guess: 74
The secret number is lower than : 74
Chances remains: 2
Enter your guess: 72
The secret number is lower than : 72
Chances remains: 1
Enter your guess: 70
The secret number is lower than : 70
Chances remains: 0
Sorry...You lost.
The secret number is: 69
Do you want to play again?
1.Yes
2.No
Choice: 2
Your average guess: 3
-- program is finished running --