首页 > 解决方案 > 程序无响应但仍在运行

问题描述

# 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. 

标签: ruby

解决方案


有一些小的逻辑错误。在你的check_value()函数中,你需要在某个时候从它返回,否则它会一直循环。您可以在ans = Integer(quantity)调用后立即返回,因为它会引发错误并转到rescue其他情况。

rescue块内,不要调用.to_i,因为这会导致字符串被强制转换为整数 0,这不是你的意图。相反,只需使用gets.chompsoquantity仍然是一个字符串并传递回要测试的循环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)

推荐阅读