2016-10-02 55 views
-1

我正在學習ruby,並且看了看網絡,看看這有什麼問題。我曾嘗試添加間距和變量之間刪除它,我路過,但我不斷收到此錯誤:意外的','語法錯誤ruby

25: syntax error, unexpected ',', expecting ')' 
student1.grades =(60,70,80) 

下面是代碼:

class Student 
    attr_accessor :name, :age 

    def initialize(name,age) 
    @name = name 
    @age = age 
    end 

    def grades(math,english,science) 
    @math = math 
    @english = english 
    @science = science 
    average_grade = (math.to_i + english.to_i + science.to_i)/3 
    return average_grade 
    end 

    def to_s 
    puts "Name = #{name}" 
    puts "Age = #{age}" 
    puts self.grades 
    end 
end 

student1 = Student.new("Tom","23") 
student1.grades = (60,70,80) 
puts student1 

回答

4

檔次接收三個參數。你不要在任務中這樣做。因此,改變

student1.grades = (60,70,80) 

student1.grades(60,70,80) 
1

,用戶可以爲學生目標成績爲

student1.grades(60,70,80) 

也輕微edits.You可以添加的方法計算平均

def grades_details 
    average_grade = (@math.to_i + @english.to_i + @science.to_i)/3 
    return average_grade 
end 

所以,當你重寫以字符串ü稱它而不是self.grades

def to_s 
    puts "Name = #{name}" 
    puts "Age = #{age}" 
    puts grades_details 
end