我的目标是让电脑找到秘密号码,而用户应该告诉电脑它是高还是低? 当它总是高或总是低时,它可以工作,但当我必须输入两个时,它就不行了。谁知道有什么解决办法?
.data
prompt: .asciiz "Enter the Secret Number"
prompt2: .asciiz "\n Computer guess is: "
higherLowerorCorrect: .asciiz "\n Number is higher(h) lower(l) or correct/exit(2):"
.text
li $v0, 4
la $a0, prompt
syscall
li $v0, 5
syscall
move $t2, $v0
li $t7, 101
li $t6, 0
li $t5, 0
loop:
move $a1, $t7
li $v0, 42
syscall
add $a0, $a0, $t6
move $t5, $a0
li $v0, 4
la $a0, prompt2
syscall
move $a0, $t5
li $v0,1
syscall
li $v0, 4
la $a0, higherLowerorCorrect #
syscall
li $v0, 12
syscall
beq $v0, 'l', setHigherBound ####
beq $v0, 'h', setLowerBound ####
beq $v0, 'x', exit ####
setHigherBound:
move $t7, $t5
j loop
setLowerBound:
add $t7,$t7,$t6
sub $t7, $t7, $t5
move $t6, $t5
j loop
exit:
output:
你计算允许计算机猜测的数字的方法在我看来有点破绽。
考虑以下情况。
我建议你把你的 setLowerBound
代码到。
setLowerBound:
addiu $t6, $t5, 1 # LB is inclusive, so set it to guess+1
j loop
和随机数的生成。
sub $a1, $t7, $t6
li $v0, 42
syscall
add $a0, $a0, $t6 # $a0 = rand(UB - LB) + LB