0

というクラスがあり、そのクラス内には、 2 つの変数を出力する puts ステートメントを持つPolynomialElementsメソッドがあります。printElementputs ステートメントは、変数を異なる行に出力しています。2 つの変数を 1 行に出力するにはどうすればよいですか。私のコードは以下のとおりです。 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
4

1 に答える 1

1

whileループの変更:

value = gets

に:

value = gets.chomp

その後、次のようになります。

Enter a value for the Polynomial 
3
1x^2
2x^1
3x^0
于 2015-10-18T17:40:44.220 に答える