为什么这个Ruby计算器无法正常工作? [英] Why isn't this Ruby calculator working as intended?

查看:82
本文介绍了为什么这个Ruby计算器无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码旨在用作基本计算器,并且尚未添加某些功能。

This code is intended to be a basic calculator with some features not yet added.

只要运行一次,就会显示两个开始的 puts 显示,但是当程序收到用户输入时,它将关闭。

Whenever this is run the two beginning puts display, but when the program receives user input it closes.

从根本上说有什么错误吗?我现在尝试调试〜1个小时,我已经放弃了。

Is there anything fundamentally wrong? I tried debugging for ~1 hour now and I have given up.

puts "Hello There. What you you like to calculate?"
puts "You have the option of Distance, Slope, Pythag, Fibonacci, and Sales Tax"
calculationSelection = gets

if calculationSelection == "Distance"
  puts "You have selected Distance"
  puts "Please enter x2"
  x2 = gets
  puts "Please enter x1"
  x1 = gets
  puts "Please enter y2"
  y2 = gets
  puts "Please enter y1"
  y1 = gets

  Distance = Math.sqrt((x2.to_f - x1.to_f)**2 + (y2.to_f - y1.to_f)**2)
  print "Your distance is: "
  puts Distance.to_s
  pause
elsif calculationSelection == "Slope"
  puts "You have selected Slope"
  puts "Enter x of Point One"
  x1 = gets
  puts "Enter y of Point One"
  y1 = gets
  puts "Enter x of Point Two"
  x2 = gets
  puts "Enter y of Point Two"
  y2 = gets

  Slope = (x1.to_f - x2.to_f) / (y1.to_f - y2.to_f)
  print "Your slope is: "
  puts Slope.to_s 
  pause
end


推荐答案

您需要将 chomp 放在获取剥离返回值:

You will need to put chomp at the end of the gets to strip the returned value:

gets.chomp

然后,您需要输入 Distance(距离)和 Slope(坡度),还需要注意案例。您还可以在条件语句中用 downcase 匹配所有内容,以消除区分大小写。

Then you will enter the "Distance" and the "Slope" you also need to watch for the case. You can also match everything with a downcase in your conditional statement to remove case sensitivity.

这篇关于为什么这个Ruby计算器无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆