ruby - 我怎样才能得到我正在计算的方程来向用户显示 a 值 * x 的二次方。当我尝试对 x 求平方时出现错误

标签 ruby equation

puts "Let's get started calculating your parabola. What is your A value?"
a = gets.chomp

puts "What is your B value?"
b = gets.chomp
puts "What is your C value?"
c = gets.chomp
x = x
2x = (x.to_i**2)

puts "Your parabola equation is 'y = " + a.to_s + 2x.to_s + " + " + b.to_s + x + " + " + c.to_s + "'. Would you like to go back to the beginning?"

最佳答案

在 Ruby 中,变量名不能以数字开头,但你做到了,2x = (x.to_i**2)。将其写为x2 = (x.to_i**2)。然后将代码中的所有 2x 替换为 x2

另一个错误是x=x,这也是无效的。希望你打错字了。也改正一下。

阅读此内容 Local Variable Names

A local variable name must start with a lowercase US-ASCII letter or a character with the eight bit set. Typically local variables are US-ASCII compatible since the keys to type them exist on all keyboards.

(Ruby programs must be written in a US-ASCII-compatible character set. In such character sets if the eight bit is set it indicates an extended character. Ruby allows local variables to contain such characters.)

A local variable name may contain letters, numbers, an _ (underscore or low line) or a character with the eighth bit set.

关于ruby - 我怎样才能得到我正在计算的方程来向用户显示 a 值 * x 的二次方。当我尝试对 x 求平方时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21589137/

相关文章:

根据第二列的值计算表达式(方程式)的 Python 程序

java - EQ Solver 字符串返回中的复杂值问题

ruby - 使用 rbenv BUILD FAILED 安装 ruby​​-2.0.0-p0

ruby - 《实用面向对象设计》一书中介绍的理解 Ruby 代码

equation - 在计算日出/日落时如何考虑观察者的高度?

Python - 求解 y 的多项式

python - 相同的方程式,Pylab 和 Octave 的不同答案

ruby - Net::SSH 连接失败

ruby-on-rails - ruby rails 3 : How can I disconnect an object from another object in the database without deleting it?

ruby-on-rails - ruby on rails 中无休止的滚动分页,每页一个查询?