วันอังคารที่ 17 พฤศจิกายน พ.ศ. 2558

ลอง RUBY WHILE + Function + class + array [ 2 ]

class Student
  def initialize(name,w,h)
    @name, @w, @h = name, w, h
  end

  def get_bmi
    bmi=@w/(@h*@h/10000)
    return bmi
  end

  def get_name
    return @name
  end

  def get_w
    return @w
  end

  def get_h
    return @h
  end

end

def find_bmi_more(std, bmi)
  count=0
  for i in (0..2)
    if std[i].get_bmi>=bmi
      count=count+1
      puts "#{std[i].get_name}  BMI more #{bmi}"
    end
  end
  puts "Have BMI more #{bmi} = #{count} P."
end
 

def setup
  data_student = [Student.new("A",59,154),
          Student.new("B",75,179),
          Student.new("C",71,182)]

  for i in (0..2)
    print " BMI of #{data_student[i].get_name} = #{data_student[i].get_bmi}" 
    print " by Weight = #{data_student[i].get_w}"
    print " and Height = #{data_student[i].get_h}"
    puts
  end

  find_bmi_more(data_student, 25)

end

setup

ไม่มีความคิดเห็น:

แสดงความคิดเห็น