ruby - 程序无响应但仍在运行
问题描述
# Function that calculate sales totals and return output
def sales_total(quantity)
if quantity >= 100
price = 8
quantity * price
elsif quantity < 100 || quantity >= 50
price = 9
quantity * price
else
price = 10
quantity * price
end
end
# Function that displays output to screen
def display(quantity)
print "\nFor #{quantity} widgets, the total is: $#{sales_total(quantity)}"
end
# Check user input
def check_value(quantity)
bool = true
while bool
begin
ans = Integer(quantity)
rescue ArgumentError
puts "Error! Invalid Number!"
print "Enter a whole number: "
quantity = gets.chomp.to_i
end
# also tried the following:
# if quantity.is_a?(Integer)
# bool = false
# else
# next
# end
end
end
# Start of program
puts "Welcome to the Widget Store!\n"
print "\nWould you like to purchase a widget? (y/n) "
user_choice = gets.chomp.downcase
while user_choice == 'y'
print "\nHow many widgets would you like? "
quantity = gets.chomp
# Valdiate if user input is a number
check_value(quantity)
# Call to sales_total
sales_total(quantity)
# Call to output
display(quantity)
# Check if user wants to continue
print "\n\nWould you like to purchase more widgets? (y/n) "
user_choice = gets.chomp.downcase
end
print "\nThanks for using our store! Come back soon!"
程序的控制台视图
Welcome to the Widget Store!
Would you like to purchase a widget? (y/n) y
How many widgets would you like? we
Error! Invalid Number!
Enter a whole number: 45
**Cursor just continues to blink but will not take keyboard input**
The program had no issues running before adding the check_value function. Not sure where I am going wrong and have tried several different solutions.
解决方案
有一些小的逻辑错误。在你的check_value()
函数中,你需要在某个时候从它返回,否则它会一直循环。您可以在ans = Integer(quantity)
调用后立即返回,因为它会引发错误并转到rescue
其他情况。
在rescue
块内,不要调用.to_i
,因为这会导致字符串被强制转换为整数 0,这不是你的意图。相反,只需使用gets.chomp
soquantity
仍然是一个字符串并传递回要测试的循环Integer()
。这就是它变得无响应的原因 - 在将字符串转换为整数 0 之后.to_i
,它通过了Integer()
测试,但没有通过设置返回check_value()
或退出while
循环bool = false
。它不需要提示输入,因为条件已经满足。
def check_value(quantity)
bool = true
while bool
begin
ans = Integer(quantity)
# Return the valid int here
return ans
rescue ArgumentError
puts "Error! Invalid Number!"
print "Enter a whole number: "
# Do not cast it with .to_i, leave it as a string
# to be tested again on the next while loop iteration
quantity = gets.chomp
end
end
end
然后当你调用check_value()
函数时,你需要将它的返回值赋回以quantity
供 in 使用sales_total()
,否则它会传递原始输入quantity
(这可能是无效的)
while user_choice == 'y'
print "\nHow many widgets would you like? "
quantity = gets.chomp
# Valdiate if user input is a number
# And store the return back to quantity
quantity = check_value(quantity)
# Call to sales_total
# using the newly validated quantity var
sales_total(quantity)
推荐阅读
- c# - 如何监控外部流程(最简单的方法)
- python - 找到两个列表 x 和 y 之间的所有配对组合,使得 y 中的所有元素都与 x 中的一个元素配对
- algorithm - 为什么这个 F# 序列表达式是立方时间而不是线性的?
- mysql - A Database Error Occurred 错误编号:1064 WH'at line 14
- c# - 如何使用一行代码为所有 SqlCommand 设置相同的 CommandTimeout?
- ios - 从错误提交的旧提交中删除 Pod 引用
- c# - OpenID Connect 不适用于 ASP.NET MVC 5
- font-awesome - FontAwesome 坏了
- javascript - 使用 gapi 在 google 登录集成中使电话号码共享成为可选
- swiftui - 使用 Xcode 11.3 在 SwiftUI 上的 json