mac 查找ruby位置
矩形的查找区域 (Finding area of the rectangle)
Given "length" and "breadth" and we have to find the area of the rectangle.
给定“长度”和“宽度”,我们必须找到矩形的面积。
The general formula to find the area of rectangle is as follows:
查找矩形区域的一般公式如下:
area = length * breadth
We can easily find the area of a rectangle by creating logic which applies the above formula. Remember, convert the string into the float instead of integer because we expect the length and breadth to be of float type so as the result.
通过创建应用上述公式的逻辑,我们可以轻松找到矩形的区域。 请记住,将字符串转换为浮点数而不是整数,因为我们希望长度和宽度为浮点数,这样才能得到结果。
Methods used:
使用的方法:
gets: It is used to display the message to the user.
gets :用于向用户显示消息。
puts: It is used to take input from the user.
puts :用于接受用户的输入。
.to_f: It used for converting a value into float type.
.to_f :用于将值转换为浮点型。
Variables used:
使用的变量:
l : It is storing length given by the user.
l :存储长度由用户指定。
w : It contains width provided by the user.
w :包含用户提供的宽度。
area : It is containing the product of length and breadth which is eventually the area of rectangle.
area :包含长度和宽度的乘积,该乘积最终是矩形的面积。
Ruby代码查找矩形区域 (Ruby code to find the area of the rectangle)
=begin
Ruby program to find Area of Rectangle.
=end
# input length and breadth, and
# convert them to float value
puts "Enter length:"
l=gets.chomp.to_f
puts "Enter width:"
w=gets.chomp.to_f
# calculating area
area=l*w
# printing the result
puts "Area of Rectangle is #{area}"
Output
输出量
First run:
Enter length:
12.3
Enter width:
5
Area of Rectangle is 61.5
Second run:
Enter length:
1.234
Enter width:
0.78
Area of Rectangle is 0.96252
翻译自: https://www.includehelp.com/ruby/find-the-area-of-the-rectangle.aspx
mac 查找ruby位置