puts 语句打印在两行上
puts statement is printing on two lines
我有一个名为 PolynomialElements
的 class,在那个 class 里面我有一个名为 printElement
的方法,它有一个打印两个变量的 puts 语句。 puts 语句在不同的行上打印变量。如何让 puts 在一行上打印两个变量。我的代码在下面,它是 puts 语句所在的第 5 行。
class PolynomialElements
attr_accessor :element, :size
def printElement
puts "#{element}x^#{size}"
end
end
askAgain = true
polyArray = Array.new
while askAgain
puts "How many numbers do you want to enter? "
numString = gets
num = numString.to_i
while num > 0
puts "Enter a value for the Polynomial "
value = gets
polyArray.push(value)
num -= 1
end
sizeOfArray = polyArray.length
polyArray.each do |x|
var = PolynomialElements.new
var.element = x
sizeOfArray -= 1
var.size = sizeOfArray
var.printElement
end
puts "Enter y to enter new number or anything else to quit"
cont = gets
if cont.chomp != "y"
askAgain = false
else
polyArray.clear
end
end
在while
循环中更改:
value = gets
至:
value = gets.chomp
您将获得:
Enter a value for the Polynomial
3
1x^2
2x^1
3x^0
我有一个名为 PolynomialElements
的 class,在那个 class 里面我有一个名为 printElement
的方法,它有一个打印两个变量的 puts 语句。 puts 语句在不同的行上打印变量。如何让 puts 在一行上打印两个变量。我的代码在下面,它是 puts 语句所在的第 5 行。
class PolynomialElements
attr_accessor :element, :size
def printElement
puts "#{element}x^#{size}"
end
end
askAgain = true
polyArray = Array.new
while askAgain
puts "How many numbers do you want to enter? "
numString = gets
num = numString.to_i
while num > 0
puts "Enter a value for the Polynomial "
value = gets
polyArray.push(value)
num -= 1
end
sizeOfArray = polyArray.length
polyArray.each do |x|
var = PolynomialElements.new
var.element = x
sizeOfArray -= 1
var.size = sizeOfArray
var.printElement
end
puts "Enter y to enter new number or anything else to quit"
cont = gets
if cont.chomp != "y"
askAgain = false
else
polyArray.clear
end
end
在while
循环中更改:
value = gets
至:
value = gets.chomp
您将获得:
Enter a value for the Polynomial
3
1x^2
2x^1
3x^0