Array help

N

Nick Wade

I am a brand spankin' new wannabe ruby programmer. The attached is my
first real program outside of the infamous Hello World app. I was told
that one who wants to start learning a program is to start out with a
simple real world program that interests me.

Because I like to golf and I keeping track of my scores is something I
do, I figured I'd give this a whirl and then build on it
later...possibly on a web page with Rails.

Anyhow, I have the prompt and storage of scores working properly but I
don't know how to add up the scores that are entered. Any help would be
greatly appreciated.

Attachments:
http://www.ruby-forum.com/attachment/4133/golfscoreentry
 
G

Glen Holcomb

I am a brand spankin' new wannabe ruby programmer. The attached is my
first real program outside of the infamous Hello World app. I was told
that one who wants to start learning a program is to start out with a
simple real world program that interests me.

Because I like to golf and I keeping track of my scores is something I
do, I figured I'd give this a whirl and then build on it
later...possibly on a web page with Rails.

Anyhow, I have the prompt and storage of scores working properly but I
don't know how to add up the scores that are entered. Any help would be
greatly appreciated.

Attachments:
http://www.ruby-forum.com/attachment/4133/golfscoreentry
I haven't looked at your code so probably shouldn't be answering this but..=
 
F

Florian Aßmann

Hi Nick,

1st of all, in Ruby you do not need to declare or define each of your
variables at the head of a method definition.

# counter=0 # no need for a counter
# round=0 # seems not to be used
# player="nick" # gets defines
# holesplayed=0 # gets defines
# score=0 # not needed

totalscore = 0

puts "What is your name"
player = gets

puts "How many holes did you play today"
# Convert to Fixnum here, so you can call .times on holesplayed.
# See http://apidock.com/ruby/Integer/times...
holesplayed = Integer gets
holesplayed.times do
puts "Enter the next hole's score"

# add up with +=, 'gets.to_i' was originally score and score was
'gets.to_i'
totalscore += gets.to_i # x += y is like x = x + y
end

puts "your total score was #{ totalscore }"


# lil bonus
puts 'How many holes did you play today?'
holes = gets.to_i

total = (1..holes).inject(0) { |total, *|
print 'Enter the next score: '
total + gets.to_i
}

printf "Your total score was %i.\n", total


Florian

Am 10.10.2009 um 01:56 schrieb Nick Wade:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top