我正在尝试制作一个保龄球计算器,从文本文档中获取保龄球分数,将其转换为数组,然后在Ruby中计算得分。到目前为止,我有这个,但当我尝试运行代码时,我得到一个语法错误。任何帮助将不胜感激!
def read_ragged_array(the_filename)
ragged_array = [nil]
File.open(the_filename, "r") do |f|
while line = f.gets
fields = line.chomp.split(" ")
row = [ ]
fields.each do |field|
row << field.to_i
end
ragged_array << row
end
end
return ragged_array
end
def frame_score(the_frame, bonus1, bonus2)
if (the_frame[0] == 10) #strike
return 10 + bonus1 + bonus2
elsif (the_frame[0] + the_frame[1] == 10) #spare
return 10 + bonus1
elsif (the_frame[0] + the_frame[1] < 10) #empty
return the_frame[0] + the_frame[1]
end
frames = read_ragged_array("game1.txt")
sco = 0
for i in 1..10
if frames[i][0] == 10 && frames[i+1][0] == 10
bonus1 = 10
bonus2 = frames[i+2][0]
elsif frames[i][0] == 10 && frames[i+1][0] < 10
bonus1 = frames[i+1][0]
bonus2 = frames[i+1][1]
elsif frames[i][0] + frames[i][1] == 10
bonus1 = frames[i+1][0]
bonus2 = 0
else
bonus1 = 0
bonus2 = 0
end
end
frame_score(frames[i], bonus1, bonus2) + sco = x
print x
end
返回的错误是:
bowling.rb:65: syntax error, unexpected end-of-input, expecting keyword_end
更新:我不再看到语法错误,但我没有输出...感谢帮助我的新手自己! (代码已更新以反映)
答案 0 :(得分:0)
您的语法错误在此行(应为f.gets
):
while line = f.getsa
最后一行end
。